简体   繁体   English

未与RESTkit映射的核心数据关系

[英]Core Data relationships not mapped with RESTkit

I am struggling to get a proper mapping of Core Data relationships using RESTkit 0.20.0-pre6. 我正在努力使用RESTkit 0.20.0-pre6获得正确的核心数据关系映射。

I want to map this JSON: 我想映射这个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: 使用我试图RKConnectionDescription描述这里 ,使用此代码:

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: 我也尝试过使用简单的RKRelationshipMapping无济于事:

[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. 我必须缺少一些简单的东西,因为这不是RESTkit的特殊情况。 Any ideas? 有任何想法吗?

I got it working. 我知道了 The trick was to add an extra attribute 'roomId' on ItemMO for the foreign key. 诀窍是在ItemMO上为外键添加一个额外的属性“ roomId”。

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

Then tell RESTkit about the relation: 然后告诉RESTkit关系:

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

It seems as if RESTkit cannot establish the relation without the extra foreign key attribute. 如果没有额外的外键属性,似乎RESTkit无法建立关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM