ios - 仅映射一对多关系的元素

标签 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;

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0xb8a9150 {DetailedErrors=(), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: comments, device, devices The representation inputted to the mapper was found to contain nested object representations at the following key paths: comment This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}

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

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)。

感谢您的帮助。

最佳答案

您创建:

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:)。

关于ios - 仅映射一对多关系的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24333554/

相关文章:

ios - MVVM - 使用闭包将 ViewModel 与数据源绑定(bind) : capture list needed?

ios - Barbuttons 正在离开位置

iphone - NSUserDefaults 值未保存 iPhone

ios - 如何在UIButton上放置图像/在哪里可以找到好的图标?

ios - 在后台执行 NSTimer

java - 如何更改日期的 json 字符串并将其格式化为仅显示日期而不显示年、月或时间

Javascript - 从 JSON 对象(数组)中提取数据

ios - 使用 Parse 保存数据,现有项目

ios - 此行的正确内存管理? (Cocos2D)

javascript - 将嵌套的 JSON 从 Rails 应用程序发送到 hapijs 应用程序