设置-使用RestKit,以及它在CoreData存储中存储数据的能力。
我正在尝试执行两个单独的GET操作:
issue/:issueId ==>返回一个issue对象,假设存在一个具有该ID的对象。issue/:issueId/comment ==>返回comment对象,属于与issueId匹配的issue。
对于第一个调用,它只会得到一个问题。只有当我在URL上传递一个额外的参数时,它才会返回注释。否则,它不会。当然,如果我请求它,那么对象就会很好地创建,并且所有的对象都正确地连接在我的core-data存储中。
我要映射的对象是这样的
@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
Issue有一个Comments集合。评论不知道他们自己的问题。
所以,我所要做的就是使这两个调用都有可能存在。
例如,在我们的url中,假设"issueId"是12345。因此,如果我调用http://example.com/issue/12345,我希望数据被写入我的CoreData存储。(顺便说一句,这个方法非常有效)。接下来我想做的是调用"http://example.com/issue/12345/comments",然后让这些评论写入CoreData存储,并连接到已经存在的问题-12345。这就是我遇到麻烦的地方。
如果有人能在这方面提供指导,我将非常感激。
在官方的repo上阅读了这个问题后,我将按照如下方式进行。
在你的核心数据模型中添加反向关系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
和它的逆)。