Question

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"
    }
]
Était-ce utile?

La 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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top