2013-01-18 39 views
2

我正努力使用RESTkit 0.20.0-pre6獲得核心數據關係的正確映射。未與RESTkit映射的核心數據關係

我想這個JSON映射:

{ "items" : [ {"id" : 2001, "itemAttr1" : "..."}, ...<more items>... ], 
    "rooms": [ {"id" : 3001, "items": [2001, ...<more item id's>...] } 

到相應的核心數據模型:

Entity ItemMO (Attributes "id", "itemAttr1", Relationship "room" to RoomMO) 
Entity RoomMO (Attributes "id", Relationship "items" to ItemMO) 

的屬性映射罰款,但關係是空的。

我一直在使用RKConnectionDescription描述here嘗試,使用此代碼:

NSEntityDescription *roomEntity = [NSEntityDescription entityForName:@"RoomMO" inManagedObjectContext:self.context]; 
NSRelationshipDescription *itemsInRoom = [roomEntity relationshipsByName][@"items"]; 
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:devicesInRoom keyPath:@"devices"]; 
[roomMapping addConnection:connection]; 

我一直在使用一個簡單的RKRelationshipMapping不得要領也嘗試:

[itemMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"room" withMapping:roomMapping]]; 

我必須失去了一些東西簡單這不應該成爲RESTkit的一個特例。有任何想法嗎?

回答

6

我明白了。 訣竅是在ItemMO上爲外鍵添加額外的屬性'roomId'。

Entity ItemMO (Attributes "id", "roomId", "itemAttr1", Relationship "room" to RoomMO) 

然後告訴RESTkit約的關係:

[itemMapping addConnectionForRelationship:@"room" connectedBy:@{@"roomId" : @"id"}]; 

看起來好像RESTkit不能建立沒有額外的外鍵屬性的關係。

+0

+1用於明確排除RESTKit在沒有額外外鍵屬性的情況下執行此映射的容量。 –