I'm trying to figure out how to realize a mapping of an API
response into CoreData objects using RestKit. The API
uses JSOG standard. Here is an example:
[
{
"@id": "1",
"name": "Sally",
"friend": {
"@id": "2",
"name": "Bob",
"friend": {
"@id": "3",
"name": "Fred",
"friend": { "@ref": "1" }
}
}
},
{ "@ref": "2" },
{ "@ref": "3" }
]
How would I create an RKEntityMapping
for such a JSON
? Mapping of simple attributes is trivial, the question is how to setup the relationships here so they work with @ref
, also, when the top level user object contains the @ref
only.
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"Question"
inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];
[userMapping addAttributeMappingsFromDictionary:@
{
@"@id" : @"identifier",
@"name" : @"name"
}];
[userMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"friend"
toKeyPath:@"friend"
withMapping:userMapping]];
My guess is that i could use code below to handle the @ref
inside of an object:
[userMapping addConnectionForRelationship:@"friend" connectedBy:@{@"@ref": @"@id"}];
but is it correct?
- How would I map either a full object or just a reference to an already-provided object (via
@ref
) to actual Core Data objects? - How can I map the top elements of the JSON (being a list of
User
objects) to an actual list ofUser
entities?