Reputation: 3844
I am struggling to get a proper mapping of Core Data relationships using RESTkit 0.20.0-pre6.
I want to map this JSON:
{ "items" : [ {"id" : 2001, "itemAttr1" : "..."}, ...<more items>... ],
"rooms": [ {"id" : 3001, "items": [2001, ...<more item id's>...] }
to a corresponding Core Data model:
Entity ItemMO (Attributes "id", "itemAttr1", Relationship "room" to RoomMO)
Entity RoomMO (Attributes "id", Relationship "items" to ItemMO)
The attributes are mapped fine, but the relationships are empty.
I have tried using RKConnectionDescription
as described here, using this code:
NSEntityDescription *roomEntity = [NSEntityDescription entityForName:@"RoomMO" inManagedObjectContext:self.context];
NSRelationshipDescription *itemsInRoom = [roomEntity relationshipsByName][@"items"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:devicesInRoom keyPath:@"devices"];
[roomMapping addConnection:connection];
I have also tried using a simple RKRelationshipMapping
to no avail:
[itemMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"room" withMapping:roomMapping]];
I must be missing something simple as this should not be an exotic case for RESTkit. Any ideas?
Upvotes: 2
Views: 2130
Reputation: 3844
I got it working. The trick was to add an extra attribute 'roomId' on ItemMO for the foreign key.
Entity ItemMO (Attributes "id", "roomId", "itemAttr1", Relationship "room" to RoomMO)
Then tell RESTkit about the relation:
[itemMapping addConnectionForRelationship:@"room" connectedBy:@{@"roomId" : @"id"}];
It seems as if RESTkit cannot establish the relation without the extra foreign key attribute.
Upvotes: 6