1
votes

Setup - using RestKit, along with it's abilities to store data in a CoreData store.

I'm trying to perform two separate GET operations:

issue/:issueId ==> this returns an Issue object, assuming one with that ID exists. issue/:issueId/comment ==> this returns Comment objects, belonging to the issue matching issueId.

So, for the first call, that just gets an issue back. It will only return comments back if I pass in an extra parameter on the URL. Otherwise, it won't. Of course, if I do ask for it, then the objects get created just fine, and all the objects are connected correctly in my core-data store.

The objects that I'm mapping look like this:

@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 has a collection of Comments. Comments don't know about their owning Issue.

So, all I'm trying to do is make it possible for both of these calls to exist.

For example, in our URLs, say "issueId" is 12345. So, if I make one call to http://example.com/issue/12345, I'd like the data to be written to my CoreData store. (This works great, btw). What I would like to happen next is to call "http://example.com/issue/12345/comments", and then have those comments write to the CoreData store, and also be connected to issue-12345, that's already there. That's the part that I'm having trouble with.

If anyone could offer guidance on this, I'd really appreciate it.

1

1 Answers

0
votes

After reading this issue on the official repo, I would proceed like follows.

In you Core Data model add the inverse relationship Comment -> Issue, so that your Comment interface looks like

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

and make that relationship mandatory.

Now you have to setup your mapping adding that relationship, for instance

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

If my understanding is correct, RestKit should populate both relationships (the one-to-many Issue -> Comment and its inverse) for you.