【问题标题】:Core Data relationships not mapped with RESTkit未使用 RESTkit 映射的核心数据关系
【发布时间】:2013-01-18 11:48:15
【问题描述】:

我正在努力使用 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 所述,使用以下代码:

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 的奇特案例。有什么想法吗?

【问题讨论】:

    标签: ios core-data restkit


    【解决方案1】:

    我让它工作了。 诀窍是在 ItemMO 上为外键添加一个额外的属性“roomId”。

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

    然后将关系告诉 RESTkit:

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

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

    【讨论】:

    • +1 用于明确排除 RESTKit 在没有额外外键属性的情况下执行此映射的能力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多