核心数据关系未与 RESTkit 映射

2024-02-07

我正在努力使用 RESTkit 0.20.0-pre6 获得核心数据关系的正确映射。

我想映射这个 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 http://restkit.org/api/latest/Classes/RKConnectionDescription.html,使用此代码:

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 的特殊情况。有任何想法吗?


我成功了。 诀窍是在 ItemMO 上为外键添加一个额外的属性“roomId”。

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

然后告诉 RESTkit 的关系:

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

如果没有额外的外键属性,RESTkit 似乎无法建立关系。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐