使用RestKit进行外键关系映射

11
我很陌生RestKit,有些困难。
JSON:
{
    "teams": [
        {
            "id": 1,
            "name": "Team A"
        },
        {
            "id": 2,
            "name": "Team B"
        }
    ],
    "users": [
        {
            "id": 1,
            "name": "cameron",
            "teamId": 1
        },
        {
            "id": 2,
            "name": "paul",
            "teamId": 2
        }
    ]
}

核心数据(CoreData):

@interface Team : NSManagedObject
@property (nonatomic, retain) NSNumber * teamId;
@property (nonatomic, retain) NSString * name;
@end




@interface User : NSManagedObject
@property (nonatomic, retain) NSNumber * userId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Team * team;
@end

我的应用逻辑看起来像这样:

// team mapping
RKEntityMapping *teamMapping = [RKEntityMapping mappingForEntityForName:@"Team" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
teamMapping.identificationAttributes = @[@"teamId"];
[teamMapping addAttributeMappingsFromDictionary:@{
 @"id": @"teamId",
 @"name": @"name"
 }];


// Register our mappings with the provider
RKResponseDescriptor *teamResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:teamMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"teams"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:teamResponseDescriptor];



// user mapping
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
userMapping.identificationAttributes = @[@"userId"];
[userMapping addAttributeMappingsFromDictionary:@{
 @"id": @"userId",
 @"name": @"name"
 }];


// Register our mappings with the provider
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"users"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:userResponseDescriptor];

我真的想不出如何让RestKit填充用户对象的team属性。

我已经看了很多帖子,但我尝试的一切都无效,这不是通常的用例吗?

有人知道如何做到这一点吗?任何帮助都将不胜感激!

谢谢。


我希望这个链接能对你有所帮助:https://dev59.com/WWYq5IYBdhLWcg3wcgPq#15607217 - user2261595
1个回答

17
你需要向你的User实体添加一个临时属性,它持有关联的teamId。这需要添加到你的userMapping中。
然后,你需要向你的userMapping添加一个关系定义:
[userMapping addConnectionForRelationship:@"team" connectedBy:@{ @"teamId": @"teamId" }];

这样做可以为RestKit提供与服务器连接所需的信息,并指示其将连接作为映射操作的一部分进行。


为了确保创建存根对象,您可能需要第二个响应描述符,该描述符导航到ID的关键路径(因为第一个响应描述符需要将ID复制到其他对象)。 - Wain
@Wain 我正在做完全相同的事情,但是每个由addConnectionForRelationship:connectedBy:指定的关系在RESTkit尝试将其持久化到Core Data时都变成了nil。这是为什么? - Matt Baker
@Wain 我已经检查了跟踪输出,在 JSON -> 实体步骤中报告说,从 JSON 到我的瞬态属性的外键值已成功映射。然后在我的 addConnectionForRelationship:connectedBy: 中引用这些瞬态属性。实际上,RESTkit 从 Core Data 返回的错误消息已填充瞬态外键属性,但是生成的关系实体却为 nil - Matt Baker
@MattBaker,你很可能需要提出一个问题,并展示你的源JSON、映射和错误信息(日志输出)。 - Wain
1
@Wain,有两个问题:1)我的几个对象缺少响应描述符,2)我映射了一个多对多链接对象的两侧,但RestKit似乎不喜欢。一旦我注释掉addConnectionForRelationship:connectedBy:@{"arrayOfIds": "objectId"},只留下关系的另一侧定义,它就很顺利了。 - Matt Baker
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接