2

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"
    }
]
brcebn
  • 1,571
  • 1
  • 23
  • 46
  • Have you looked in RKConnectionDescription. It lets you map the comments independently to the parent model. How does your JSON look. If you send me the sample, I could possibly provide you with answer. – Sandeep May 15 '14 at 12:16
  • I've added an typical `JSON` response. If I map those comments independently to the parent, How can I join them to the parent after the mapping ? Thank you for your so fast answer anyway. – brcebn May 15 '14 at 12:19

2 Answers2

0

Say your have the JSON like above, you will have to add an attribute to correspond to which event a particular comment belongs to such that our json would look like,

"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",
        "event_id": "10", /* Note this is a new attribute to map to the parent entity */
    }
]

Now, add a new attribute to your comment model, such that this event_id will be stored. We create an attribute eventId.

So, lets us create the mapping for the entity,

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Comment" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[mapping setIdentificationAttributes:@[@"identifier"]];
[mapping addAttributeMappingsFromDictionary:@{
     @"id": @"identifier",
     @"updated_at": @"updatedAt",
     @"created_at": @"createdAt",
     @"user_id": @"userId",
     @"commentable_id": @"commentableId",
     @"commentable_type": @"commentableType",
     @"content": @"content",
     @"event_id": @"eventId"
   }];

Then we will have to add connection to map the event_id to correct event,

NSRelationshipDescription *eventRelationship = [[mapping entity] relationshipsByName][@"event"];

[mapping addConnection:[[RKConnectionDescription alloc] initWithRelationship:eventRelationship attributes:@{@"eventId": @"identifier"}]];  // this line says that you have to have eventId in your comments entity and then the identifier in event entity to which the eventId will be mapped to.

And that's it, you could now create RKResponseDescriptor and RKManagedObjectRequestOperation to pull the comment and it will point to correct event.

Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • Thank you for your answer. That's the good mapping. But my main problem is how to link that to an existing object. I agree that this mapping is require. – brcebn May 15 '14 at 12:54
  • It will be linked to existing events, once you fetch all the comments again with this new connection description. – Sandeep May 15 '14 at 12:56
0

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Additional info: http://stackoverflow.com/questions/17326087/foreign-key-relationship-mapping-with-restkit – Wain May 15 '14 at 12:35
  • I knew that I could use foreign key mapping but is it possible to add manually the relationship to an existing object ? I have something like that is mine: `event.comments = comments_array` where `comments_array` is the new mapping of my `JSON` request and `event` an existing object? Basically I don't really want to reload the whole event when I'm reloading comments' event. – brcebn May 15 '14 at 12:51
  • Close. Your managed object subclass should have some `NSSet` methods that were added for the relationship. You can create an `NSSet` from the `NSArray` you get from the mapping result and use that to populate the relationship. You only need this if you don't use the foreign key mapping. – Wain May 15 '14 at 13:10
  • That means (like said @insane-36 I believe) the foreign mapping can work even if the mapping is split in the time (an object and a relationship after)? I'll try it and let you know if I have some more questions. Thank you! – brcebn May 15 '14 at 13:35
  • If you add the foreign key mapping on each mapping then the order you request the data in doesn't matter. The whole point of foreign key mapping is that the requests are made separately (if they weren't you would get one response and use nested mappings). – Wain May 15 '14 at 14:02
  • That was my main question. Thank you! I'm working on it. I'll let you know the result on my work. – brcebn May 15 '14 at 14:32