RestKit - Nil在嵌套对象中映射以获得关系

时间:2014-04-28 14:58:01

标签: ios core-data restkit

我从服务器端点获取以下JSON:

{
   (...),
   "comments":[
      {
         "user":{
            "account":"free",
            "name":"BrucWayne",
            "counter":{
               "pictures":10,
               "following":0,
               "followers":0,
               "tags":0
            },
            "id":"QYgZb"
         },
         "text":"How are you?",
         "date":"2014-04-28T16:22:47+0200"
      },
      {
         "user":{
            "account":"free",
            "name":"DevAbdul",
            "counter":{
               "pictures":19,
               "following":0,
               "followers":0,
               "tags":1
            },
            "id":"AbADE"
         },
         "text":"ALA_MA_KOTA2",
         "date":"2014-04-28T16:25:10+0200"
      }
   ]
    ,(...)
}

我希望将评论对象保存到CoreData结构中并保持正确的关系。我在{.xcdatamodel文件中将CommentUser个对象模型定义为NSManagedObject实例:

注释:

enter image description here

用户:

enter image description here

这是我的Comments对象的映射代码:

-(void) mapComment
{
     commentsMapping = [RKEntityMapping mappingForEntityForName:@"Comment" inManagedObjectStore:[self managedObjectStore]];
    [commentsMapping setIdentificationAttributes:@[@"date",@"text"]];
    [commentsMapping addAttributeMappingsFromDictionary:@{@"text" : @"text",
                                                          @"@metadata.routing.parameters.pictureId" : @"pictureId",
                                                          @"user.id" : @"userId",
                                                          @"user.name" :@"userName",
                                                                 @"date" : @"date"}];

    [commentsMapping addConnectionForRelationship:@"user" connectedBy:@"userId"];
    [commentsMapping addConnectionForRelationship:@"picture" connectedBy:@"pictureId"];



    RKResponseDescriptor *commentShutReponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:commentsMapping method:RKRequestMethodAny pathPattern:@"/v1/pictures/:pictureId/comments" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [[self objectManager] addResponseDescriptor:commentReponseDescriptor];

     RKObjectMapping *commentRequestMapping = [RKObjectMapping requestMapping];
    [commentRequestMapping addAttributeMappingsFromDictionary:@{@"text" : @"text"}];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:commentRequestMapping objectClass:[Comment class] rootKeyPath:nil method:RKRequestMethodAny];
    [self.objectManager addRequestDescriptor:requestDescriptor];


    //Routes
    RKRoute *commentPictureRelationshipRoute = [RKRoute routeWithClass:[Comment class] pathPattern:@"/v1/pictures/:pictureId/comments" method:RKRequestMethodAny];
    [[[[self objectManager] router] routeSet] addRoute:commentPictureRelationshipRoute];
}

我对此配置的问题是映射没有按预期工作。我设法正确映射了Picture对象,但遗憾的是我无法让RestKit映射User对象。 RestKit解析后,所有用户引用都等于nil。我想配置关系,以便正确识别用户对象并将其作为新的User对象映射到CoreData中,并在其自身和Comment实例之间建立正确的关系。 我还想提一下,这种关系是一对多的关系。

1 个答案:

答案 0 :(得分:0)

您目前对评论和连接的评价很好。

您需要添加的是User的新映射和新的响应描述符,以便可以处理用户。然后,一旦映射了用户和注释,就会建立连接。

唯一重要的信息是在响应描述符上使用的关键路径,应该comments.user向下钻取到源数据中的正确级别以执行映射。