I've been researching how to go about connecting relationships in Core Data using RestKit 0.22.0 and am wondering about mapping partially formed entities.
The API I am using returns JSON similar to that shown below. Notice that the array of tracks is returning dictionary objects which contain the foreign key id to the track.
{
"code": 200,
"data": [
{
"active": true,
"tracks": [
{
"collection": "tracks",
"id": "00000000000000000000000000"
},
{
"collection": "tracks",
"id": "11111111111111111111111111"
}
],
"created_at": "2014-04-25 07:00:07.264000",
"id": "abcdefghijklmnop1234567890",
"title": "My Playlist",
"updated_at": "2014-04-25 16:44:39.390000",
"weight": 0.0
}
],
"info": {
"limit": null,
"skip": null,
"total": 1
}
}
The data model looks like the following (simplified for demonstration):
The entity mapping for the playlist currently includes the following code to setup the relationship:
[playlistEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"tracks" toKeyPath:@"tracks" withMapping:trackEntityMapping]];
Then I can hit API to retrieve the fully-formed object. This seems to work ok for GET requests and it is using the same managed objects in core data with identificationAttributes setup. However, this doesn't smell quite right. If I try to use the inverse mapping to PATCH objects on the server with any locally modified relationships RestKit will create the fully-formed/nested JSON structure to send to the server instead of simply using foreign keys.
I've done my research and found many helpful resources like:
- http://restkit.org/api/latest/Classes/RKEntityMapping.html#//api/name/addConnectionForRelationship:connectedBy:
- http://restkit.org/api/latest/Classes/RKConnectionDescription.html
- Restkit - Core Data One-To-One Relationship Mapping via Foreign Key
- Foreign key relationship mapping with RestKit
... and others.
It looks like RKConnectionDescription could help me cull the data. The RKConnectionDescription documentation includes an example where a collection of ids are stuffed into a transient property on the entity. I'd love to see an example of RKConnectionDescription in action if there reference examples beyond the snippets from the docs. This seems like such a powerful tool in the RestKit arsenal, perhaps deserving of some sample code or an in-depth exploration in the wiki.
Can anyone point me in the right direction to connecting these relationships correctly?