【问题标题】:RestKit - Nil being mapped in nested object for relationshipsRestKit - Nil 被映射到嵌套对象中以建立关系
【发布时间】:2014-04-28 14:58:01
【问题描述】:

我正在从我的服务器端点获取以下 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"
      }
   ]
    ,(...)
}

我希望将 cmets 对象保存到 CoreData 结构中,并保持正确的关系。我在我的 *.xcdatamodel 文件中将 Comment 和 User 对象模型定义为 NSManagedObject 实例:

评论:

用户:

这是我的 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。我想配置关系,以便在 CoreData 中正确识别用户对象并将其映射为新的用户对象,并在其自身与 Comment 实例之间具有正确的关系。 我还想提一下,这种关系是一对多的关系。

【问题讨论】:

  • 所以问题是用户在请求之前不存在,你想同时创建和链接他们?

标签: ios core-data restkit


【解决方案1】:

您目前拥有的内容可以用于评论和连接。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    相关资源
    最近更新 更多