仅映射一对多关系的元素

时间:2014-06-20 18:30:37

标签: ios objective-c json mapping restkit

我遇到了以下问题。我在EventComment之间存在关系one_to_many。一个Event可以包含多个Comment,但Comment只有一个Event

直到这里,一切都很好。现在,当我添加评论时,我想仅映射这个新评论。这意味着我正在使用从CommentMoment的关系。

我遇到了一些我无法解决的映射问题。在所有描述之后,我的错误发生在本文末尾。

我收到了这个JSON:

"comment": {
    "id": 17,
    "commentable_id": 12,
    "commentable_type": "Moment",
    "content": "That's it ! ",
    "created_at": "2014-06-20T18:17:42Z",
    "updated_at": "2014-06-20T18:17:42Z",
    "user_id": 1,
    "creator": {
        "id": 1,
        "email": "test@test.com",
        "firstname": "Bobby",
        "lastname": "Stouket",
        "gender": 0,
        "created_at": "2014-04-06T17:48:11Z",
        "updated_at": "2014-06-20T18:17:26Z"
    }
}

这是我的评论映射:

RKEntityMapping *commentMapping = [RKEntityMapping mappingForEntityForName:@"Comment" inManagedObjectStore:store];
commentMapping.identificationAttributes = @[ @"commentId"];
[commentMapping addAttributeMappingsFromDictionary:@{
                                                     @"id" : @"commentId",
                                                     @"updated_at": @"updatedAt",
                                                     @"created_at": @"createdAt",
                                                     @"user_id": @"userId",
                                                     @"commentable_id": @"commentableId",
                                                     @"commentable_type": @"commentableType",
                                                     @"content": @"content"
                                                     }];

RKEntityMapping *userCreatorMapping = [APICallUser RKGetUserMappingOnlyWithAvatarForManagedObjectStore:store];
[commentMapping addConnectionForRelationship:@"creator" connectedBy:@{@"userId": @"userId"}];
[commentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"creator"
                                                                              toKeyPath:@"creator"
                                                                            withMapping:userCreatorMapping]];

这是我的时刻映射的代码(与评论的关联起作用):

RKEntityMapping *momentMapping = [RKEntityMapping mappingForEntityForName:@"Moment" inManagedObjectStore:store];
momentMapping.identificationAttributes = @[ @"momentId"];
[momentMapping addAttributeMappingsFromDictionary:@{
                                                          @"id" : @"momentId",
                                                          @"creator.id" : @"creatorId",
                                                          @"created_at" : @"createdAt",
                                                          @"updated_at" : @"updatedAt"
                                                          }];

RKEntityMapping *commentMapping = [APICallComment RKGetCommentMappingForManagedObjectStore:store];
[commentMapping addConnectionForRelationship:@"moment" connectedBy:@{@"commentableId":@"momentId"}];
[momentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"comments"
                                                                              toKeyPath:@"comments"
                                                                            withMapping:commentMapping]];

还有一件事需要知道,评论可以在片刻或照片上。根据我的JSON,我认为我不需要RKDynamicMapping,但我不确定。

这是我使用映射时的代码。请求发送成功,我收到之前写的JSON。

KEntityMapping *commentMapping = [APICallComment RKGetCommentMappingForManagedObjectStore:self.appDelegate.managedObjectStore];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:commentMapping
                                                                                        method:RKRequestMethodPOST
                                                                                   pathPattern:APICallCommentCreateCommentsRouteName
                                                                                       keyPath:@"comment"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[session.objectManager addResponseDescriptor:responseDescriptor];
//session.objectManager.requestSerializationMIMEType=RKMIMETypeJSON;
  

错误Domain = org.restkit.RestKit.ErrorDomain Code = 1001“在搜索到的关键路径中找不到可映射的对象表示。” UserInfo = 0xb8a9150 {DetailedErrors =(),NSLocalizedFailureReason =映射操作无法在搜索的关键路径中找到任何嵌套对象表示:注释,设备,设备   发现输入到映射器的表示包含以下关键路径的嵌套对象表示:注释   这可能表示您错误配置了映射的关键路径。,NSLocalizedDescription =在搜索的关键路径中找不到可映射的对象表示。,keyPath = null}

修改 以下是代码行session.objectManager.requestDescriptor的结果。这真的很奇怪。我只能看到NSArray中的一个对象。当我打印它时,我可以阅读:

Printing description of $1:
<__NSArrayI 0xbd61010>(
<RKRequestDescriptor: 0xbd12bb0 method=(POST) objectClass=BasicLocation rootKeyPath=position :      <RKObjectMapping:0xbd40b70 objectClass=NSMutableDictionary propertyMappings=(
"<RKAttributeMapping: 0xbd545d0 latitude => lat>",
"<RKAttributeMapping: 0xbd58430 longitude => lng>"
)>>
)

我从未写过position应该是rootKeyPath而我的其他属性不在这里(content,commentableType,userId,createdAt,updatedAt,commentId)。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您创建:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:commentMapping
                                                                                    method:RKRequestMethodPOST
                                                                               pathPattern:APICallCommentCreateCommentsRouteName
                                                                                   keyPath:@"comment"
                                                                               statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

但您无法将其添加到对象管理器,因为它只能理解comments, device, devices

这似乎是你的主要问题。

你通常不会这样做:

[commentMapping addConnectionForRelationship:@"creator" connectedBy:@{@"userId": @"userId"}];
[commentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"creator"
                                                                          toKeyPath:@"creator"
                                                                        withMapping:userCreatorMapping]];

因为您提供了2个不同的映射,用于完全相同的内容和关系,您只需要一个,因为用户信息嵌套在注释信息中。因此,您可以删除外键映射(addConnectionForRelationship:)。

答案 1 :(得分:0)

映射很好,但错误来自这里:

[RKResponseDescriptor responseDescriptorWithMapping:commentMapping
                                             method:RKRequestMethodGET
                                        pathPattern:HERE
                                            keyPath:@"comment"
                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

我没有写路径模式。这个变量改变了,一切都很完美。