Search code examples
objective-ccore-datamappingrestkit

CoreData and RestKit


I have a simple question about logic to use between CoreData and RestKit.

I'm using RestKit to map my JSON responses to CoreData entities. I have an Event with comments. I'm sending a request to get information about event and a second one (for now) for the comments.

Is there a way to map comments independently of my previous event and join them after or Do I have to join them with the event when there is the mapping? I don't know what is the best way to do this.

In my future implementations, I would like to send to get information from the event and its comments. But I still want to keep my secondary method to get comments without getting the whole event.

"comments": [
    {
        "id": 23,
        "user_id": 9,
        "commentable_id": 12,
        "commentable_type": "Event",
        "content": "This is the content of the event",
        "created_at": "2013-04-19 19:28:42.533901",
        "updated_at": "2013-04-19 19:28:42.533901"
    }
]

Solution

  • You can get RestKit to connect the relationship between the objects using foreign key mapping (RKConnectionDescription as per the comment from @insane-36). If the appropriate destination object is available in the context then the connection will be made, if it isn't then nothing will be done.

    If you don't want to do that then you would need to write code to duplicate that task, i.e. fetch the comments, iterate over them, fetch the associated events, connect the relationships, save.

    Having RestKit connect the relationships is certainly a lot easier for you and it doesn't preclude you requesting comments where you don't have the event that they should be attached to.

    Note that you can specify the foreign key mapping on both the event and the comment mapping so that whichever is loaded first RestKit will connect to the other when the latter mapping is processed.