Question

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):

Simplified Core Data Model

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:

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

Was it helpful?

Solution

You can't use the foreign key capability of RestKit for serialisation because it's designed for mapping incoming data. I haven't tried, but I wouldn't expect the connection to be reversed when taking the inverse of a mapping, because it is just using the information from other mappings (usually to transient variables) to facilitate relationship establishment.

So, what you should be doing is creating a custom mapping (rather than an inverse) which extracts only the information you want from the relationship (the identity) and using that mapping for your serialisation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top