2014-04-28 92 views
1

我从我的服务器端点得到以下JSON:RestKit - 无映射在嵌套的对象关系

{ 
    (...), 
    "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结构正确关系的评论对象perserved。我在* .xcdatamodel文件定义为NSManagedObject实例CommentUser对象模型:

评论:

enter image description here

用户:

enter image description here

这里是我的映射评论对象代码:

-(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中被正确识别并映射为一个新的User对象,并在它自己和Comment实例之间建立正确的关系。 我也想提一下,这种关系是一对多的关系。

+0

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

+0

@是的,确切地说。 – Paul

回答

0

你目前对评论和连接是否正确。

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

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