RestKit 添加属性映射和关系映射

Posted

技术标签:

【中文标题】RestKit 添加属性映射和关系映射【英文标题】:RestKit Add Property Mapping and Relationship Mapping 【发布时间】:2014-01-29 11:32:06 【问题描述】:

请指导我解决以下问题。

我有两个实体,如下图所示

我在 ios 7 中使用最新版本的 RestKit

现在在我的 appDelegate 中,我正在为“列表”实体使用以下映射

    NSDictionary *listObjectMapping = @
                                      @"listID" : @"listID",
                                      @"listName" : @"listName",
                                      @"listSyncStatus" : @"listSyncStatus"
                              ;

RKEntityMapping *listEntityMapping = [RKEntityMapping mappingForEntityForName:@"List" inManagedObjectStore:managedObjectStore];
[listEntityMapping addAttributeMappingsFromDictionary:listObjectMapping];

listEntityMapping.identificationAttributes = @[ @"listID" ];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:listEntityMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:@"/api/lists"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];


//Inverse mapping, to perform a POST
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[listEntityMapping inverseMapping]
                                                                               objectClass:[List class]
                                                                               rootKeyPath:nil
                                                                                    method:RKRequestMethodPOST];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/json"];
[objectManager addRequestDescriptor:requestDescriptor];



//Inverse mapping, to perform a PUT
requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[listEntityMapping inverseMapping]
                                                                               objectClass:[List class]
                                                                               rootKeyPath:nil
                                                                                    method:RKRequestMethodPUT];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/json"];
[objectManager addRequestDescriptor:requestDescriptor];

并为我的任务对象使用以下映射

    NSDictionary *taskObjectMapping = @
                                    @"listID" : @"listID",
                                    @"taskID" : @"taskID",
                                    @"taskName" : @"taskName",
                                    @"taskCompletionStatus" : @"taskCompletionStatus",
                                    @"taskSyncStatus" : @"taskSyncStatus"
                                ;

RKEntityMapping *taskEntityMapping = [RKEntityMapping mappingForEntityForName:@"Task" inManagedObjectStore:managedObjectStore];
[taskEntityMapping addAttributeMappingsFromDictionary:taskObjectMapping];
taskEntityMapping.identificationAttributes = @[ @"taskID" ];


RKResponseDescriptor *taskResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:taskEntityMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:@"/api/list/:id"
                                                                                           keyPath:nil
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:taskResponseDescriptor];


//Inverse mapping, to perform a POST
RKRequestDescriptor *taskRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[taskEntityMapping inverseMapping]
                                                                               objectClass:[Task class]
                                                                               rootKeyPath:nil
                                                                                    method:RKRequestMethodPOST];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/json"];
[objectManager addRequestDescriptor:taskRequestDescriptor];



//Inverse mapping, to perform a PUT
taskRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[taskEntityMapping inverseMapping]
                                                          objectClass:[Task class]
                                                          rootKeyPath:nil
                                                               method:RKRequestMethodPUT];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/json"];
[objectManager addRequestDescriptor:taskRequestDescriptor];

现在我的问题是如何在这两个实体之间添加关系映射? 什么是正确的方法?

如果我使用我使用以下代码行

[taskEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"list.listID"
                                                                                  toKeyPath:@"listID"
                                                                                withMapping:listEntityMapping]];

出现运行时错误,提示“无法为 keyPath listID 添加映射,已存在一个

如果我用这个

[listEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"listID"
                                                                                      toKeyPath:@"list.listID"
                                                                                    withMapping:taskEntityMapping]];

应用程序因此错误而崩溃实体列表不符合键“列表”的键值编码。'

如果我省略上面的代码并尝试从关系中获取 listID

task.list.listID 

它给了我“0”。谁能告诉我我到底做错了什么或者我应该怎么做才能完成上述任务。如果需要,我可以提供更多详细信息。

编辑

但我对所有列表的请求返回以下 json

GET www.mydomain.com/api/lists

[
"listID":"42","listName":"List 4","listSyncStatus":"1",
"listID":"41","listName":"List 3","listSyncStatus":"1",
"listID":"40","listName":"List 2","listSyncStatus":"1"
]

并且对单个列表的请求将返回其任务如下

GET www.mydomain.com/api/list/42
[
"taskID":"22","listID":"42","taskName":"Task 2","taskSyncStatus":"1","taskCompletionStatus":"1",

"taskID":"21","listID":"42","taskName":"Task 1","taskSyncStatus":"1","taskCompletionStatus":"1"
]

即在 json 中返回没有级联关系。这是错误的方式还是我在这里错过了什么?

接受答案后更正

原来我返回了错误的 json,即返回的 json 没有关系,而 iOS 模型有关系“任务”,所以我编辑了我的 rest api 以返回正确的嵌套 json,如下所示

    [  "listID" : "96",
    "listName" : "List 1",
    "listSyncStatus" : "1",
    "tasks" : [  "taskCompletionStatus" : "1",
          "taskID" : "67",
          "taskName" : "Task 2",
          "taskSyncStatus" : "1"
        ,
         "taskCompletionStatus" : "1",
          "taskID" : "66",
          "taskName" : "Task 1",
          "taskSyncStatus" : "1"
        
      ]
  ,
   "listID" : "97",
    "listName" : "List 2",
    "listSyncStatus" : "1",
    "tasks" : [  "taskCompletionStatus" : "1",
          "taskID" : "69",
          "taskName" : "Task 1",
          "taskSyncStatus" : "1"
        ,
         "taskCompletionStatus" : "1",
          "taskID" : "68",
          "taskName" : "Task 1",
          "taskSyncStatus" : "1"
        
      ]
  
]

返回嵌套的 json 之后,一切都像魅力一样,特别是这一行

[listEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"tasks"
                                                                                  toKeyPath:@"tasks"
                                                                                withMapping:taskEntityMapping]];

希望这有助于像我这样的人掌握关系概念。

【问题讨论】:

【参考方案1】:

你的模态图表明你有一对多的关系(一个列表有很多任务)。

据我所知,在这种情况下,您只需要在 List 实体上添加关系映射,不需要在 Task 实体上。同样对于一对多关系,您不需要在Task实体下添加list关系。

您的实体关系应如下所示

所以尝试关注

[listEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"YOUR_JSON_KEYPATH_HERE" toKeyPath:@"tasks" withMapping: taskEntityMapping]];

重要

以上方法

FromKeyPath parameter 应该是您的 JSON key 的名称,关系开始的地方。

toKeyPath parameter 应该是你在实体图中提到的relationship name。 IE; tasks

withMapping应该是多个实体的映射。在你的情况下taskEntityMapping

希望这能解决问题。

【讨论】:

iOS 建议应该有反比关系。 @Umair,谢谢你是对的:)。删除反向关系的建议是,尽可能少地编写代码。这样调试就很容易了。如果现在一切正常,您可以根据需要添加反向关系。这就是我喜欢调试我的代码的方式:)【参考方案2】:

不知道为什么这不起作用,但您可以尝试像这样创建关系映射:

[taskEntityMapping addConnectionForRelationship:@"list" connectedBy:@ @"listId": @"listId" ];
[listEntityMapping addConnectionForRelationship:@"tasks" connectedBy:@ @"listId":@"listId"];

【讨论】:

以上是关于RestKit 添加属性映射和关系映射的主要内容,如果未能解决你的问题,请参考以下文章

Restkit 映射 - 使用嵌入式对象/关系作为标识属性

RestKit 0.20:restkit 对象映射使属性映射加倍

Restkit:来自 JSON parentId 的映射关系

手动映射 JSON 对象时,RestKit 关系未映射

RestKit 直接 + 反向关系映射

RestKit – 使用关系映射将单个源映射到多个目的地