【问题标题】:How do I make AFIncrementalStore post data to Rails as objects?如何使 AFIncrementalStore 将数据作为对象发布到 Rails?
【发布时间】:2013-06-21 20:47:12
【问题描述】:

我有一个 iOS + Rails 3.1 应用程序,我正在使用 AFIncrementalStore 进行客户端-服务器通信。它是一种日历应用程序,Activity 是主要模型。

当我使用 Rails Web 表单创建新的 Activity 时,服务器会收到以下信息:

Started POST "/activities" for 127.0.0.1 at 2013-06-21 22:38:04 +0200
  Processing by ActivitiesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pruaVLfUijjNrYfh17yJmQgYLrnrA713OjgdayudZAg=", "activity"=>{"text"=>"Test från web", "starts_at_formatted"=>"23:00"}, "commit"=>"Create Activity"}

但是当我从我的 iOS 应用发布时,它看起来像这样:

Started POST "/activities" for 127.0.0.1 at 2013-06-21 22:36:10 +0200
  Processing by ActivitiesController#create as JSON
  Parameters: {"activityID"=>"0", "auth_token"=>"xkT2eqqdoNp5y4vQy7xA", "ends_at"=>nil, "starts_at"=>"2013-06-21T22:36:10+0200", "text"=>"Inserted!", "updated_at"=>nil}
WARNING: Can't verify CSRF token authenticity

即我缺少包装 Activity 数据的"activity"=> {...} 位。我该如何实现这一点,我是否需要在我的 AFRESTClient <AFIncrementalStoreHTTPClient> 子类中对 representationOfAttributes 进行大修?

【问题讨论】:

    标签: afnetworking afincrementalstore


    【解决方案1】:

    你需要在你的AFRestClient子类中实现-representationOfAttributes:ofManagedObject:来包装默认返回的字典。

    如果默认属性映射已经生效,可能就这么简单:

    - (NSDictionary *)representationOfAttributes:(NSDictionary *)attributes ofManagedObject:(NSManagedObject *)managedObject
    {
        return @{[managedObject.entity.name lowercaseString]: [super representationOfAttributes:attributes ofManagedObject:managedObject]};
    }
    

    如果您需要进行一些自定义属性映射,只需执行此操作即可代替我对 super 的调用。

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 1970-01-01
      • 2020-04-25
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多