【问题标题】:restkit - having trouble mapping relationships... or somethingrestkit - 映射关系有问题......或其他东西
【发布时间】:2013-03-19 13:35:22
【问题描述】:

设置 - 使用 RestKit,以及它在 CoreData 存储中存储数据的能力。

我正在尝试执行两个单独的 GET 操作:

issue/:issueId ==> 这将返回一个问题对象,假设存在具有该 ID 的对象。 issue/:issueId/comment ==> 这会返回 Comment 对象,属于与 issueId 匹配的问题。

所以,对于第一次通话,这只是一个问题。如果我在 URL 上传递一个额外的参数,它只会返回 cmets。否则,它不会。当然,如果我确实要求它,那么对象就可以很好地创建,并且所有对象都在我的核心数据存储中正确连接。

我正在映射的对象如下所示:

@interface Issue : NSManagedObject
@property (nonatomic) int32_t issueId;
@property (nonatomic, retain) NSSet* comments;
// many other fields not shown.
@end


@interface Comment: NSManagedObject 
@property (nonatomic) int32_t commentId;
// many other fields not shown.
@end

问题有一组评论。评论不知道他们拥有的问题。

所以,我要做的就是让这两个调用都存在。

例如,在我们的 URL 中,假设“issueId”是 12345。因此,如果我调用 http://example.com/issue/12345,我希望将数据写入我的 CoreData 存储。 (这很好用,顺便说一句)。接下来我想调用“http://example.com/issue/12345/comments”,然后让这些 cmets 写入 CoreData 存储,并连接到已经存在的 issue-12345。这就是我遇到问题的部分。

如果有人能提供这方面的指导,我将不胜感激。

【问题讨论】:

    标签: ios core-data restkit


    【解决方案1】:

    在官方 repo 上阅读this issue 后,我将按如下方式进行。

    在你的 Core Data 模型中添加逆向关系Comment -> Issue,这样你的Comment 界面看起来像

    @interface Comment: NSManagedObject 
    @property (nonatomic, retain) Issue * issue;
    @property (nonatomic) int32_t commentId;
    // many other fields not shown.
    @end
    

    并使这种关系成为强制性的。

    现在您必须设置映射添加该关系,例如

    [issueMapping addRelationshipMappingWithSourceKeyPath:@"comments"
                                                  mapping:[self commentMapping]];
    

    如果我的理解是正确的,RestKit 应该为您填充这两种关系(一对多的Issue -> Comment 及其逆)。

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多