使用RestKIT的具有父子关系的POST对象

时间:2014-01-31 10:45:19

标签: ios http-post restkit restkit-0.20

我使用RestKit通过网络将数据发布到我的Rest API:

        [[RKObjectManager sharedManager] postObject:list path:@"api/lists" parameters:nil
        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)

我发布的对象list有另外一个名为tasks的数据,它位于其他表中,即标准的一对多关系。现在,当我看到POST请求的日志时,它的正文仅包含list对象数据,如下所示

request.body={"listID":0,"listName":"List 3","listSyncStatus":false}

但没有相关的任务数据。我的问题是如何发布整个“列表”对象及其相关任务?任何人都可以指导我什么是正确的方式?

应用程序委托类中的我的请求描述符如下

    //INVERSE MAPPING TO PERFORM POST FOR LIST
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[listEntityMapping inverseMapping]
                                                                               objectClass:[List class]
                                                                               rootKeyPath:nil
                                                                                    method:RKRequestMethodPOST];

这是我的listEnityMapping

//LIST RELATED REQUESTS
NSDictionary *listObjectMapping = @{
                                      @"listID" : @"listID",
                                      @"listName" : @"listName",
                                      @"listSyncStatus" : @"listSyncStatus"
                                  };

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

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

我的关系映射如下:

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

我传入的json数据以这种方式格式化

GET www.mydomain.com/api/lists

[ { "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"
        }
      ]
  }
]

LOG TRACE 我知道可能是什么问题。 在执行帖子

之前出现了日志跟踪
    Mapping values from object <List: 0xc1332a0> (entity: List; id: 0xc026ec0 <x-coredata://8F498A46-254D-49F3-BEF1-0B675E5AE9D9/List/p1> ; data: {
    listID = 0;
    listName = "List 1";
    listSyncStatus = 0;
    tasks =     (
        "0xc030f30 <x-coredata://8F498A46-254D-49F3-BEF1-0B675E5AE9D9/Task/p1>"
    );
}) ((null)) to object {
} with object mapping (null)

    2014-01-31 17:02:30.229 RKGist[2455:70b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'listID' to 'listID'
2014-01-31 17:02:30.230 RKGist[2455:70b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'listID' to 'listID'. Value: 0
2014-01-31 17:02:30.230 RKGist[2455:541f] D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'NSMutableDictionary': {
    fileHFSFlags =     {
        isPrimitive = 1;
        keyValueCodingClass = NSNumber;
        name = fileHFSFlags;
    };
    fileHFSResourceForkSize =     {
        isPrimitive = 1;
        keyValueCodingClass = NSNumber;
        name = fileHFSResourceForkSize;
    };
}

    2014-01-31 17:02:30.230 RKGist[2455:70b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'listName' to 'listName'
    2014-01-31 17:02:30.231 RKGist[2455:70b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'listName' to 'listName'. Value: List 1
    2014-01-31 17:02:30.231 RKGist[2455:70b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'listSyncStatus' to 'listSyncStatus'
    2014-01-31 17:02:30.231 RKGist[2455:70b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'listSyncStatus' to 'listSyncStatus'. Value: 0
    2014-01-31 17:02:30.232 RKGist[2455:70b] D 2014-01-31 17:38:29.514 RKGist[2686:70b] D 

restkit.network:RKObjectParameterization.m:128 Serialized __NSCFNumber value at keyPath to __NSCFBoolean (0)
2014-01-31 17:38:29.514 RKGist[2686:70b] D restkit.object_mapping:RKMappingOperation.m:1021 Finished mapping operation successfully...
2014-01-31 17:38:29.516 RKGist[2686:70b] T restkit.network:RKObjectRequestOperation.m:178 POST 'http://mydomain.com/api/lists':
request.headers={
    Accept = "application/json";
    "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
    "Content-Type" = "application/json; charset=utf-8";
    "User-Agent" = "RKGist/1.0 (iPhone Simulator; iOS 7.0.3; Scale/2.00)";
}
request.body={"listID":0,"listName":"List 1","listSyncStatus":false}
2014-01-31 17:38:30.227 RKGist[2686:3603] T restkit.network:RKObjectRequestOperation.m:248 POST 'http://mydomain.com/api/lists' (200 OK / 1 objects) [request=0.7102s mapping=0.0012s total=0.7119s]:
response.headers={
    "Cache-Control" = "no-cache, no-store, must-revalidate";
    Connection = "Keep-Alive";
    "Content-Length" = 0;
    "Content-Type" = "application/json";
    Date = "Fri, 31 Jan 2014 12:38:30 GMT";
    "Keep-Alive" = "timeout=5, max=384";
    Server = Apache;
    "X-Powered-By" = "Fat-Free Framework";
}
response.body=
2014-01-31 17:38:30.228 RKGist[2686:70b] Success

即问题是当使用这个字典进行逆映射时没有找到任何关系

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

我的问题是应该添加到这个字典中,以便逆映射与任务成功发生?当前列表元素已正确映射但没有关联的任务。

1 个答案:

答案 0 :(得分:2)

根据提供的信息,您的问题是listEntityMapping(或从其创建的逆映射)没有关系告诉RestKit它也需要映射连接的对象。

RestKit仅适用于所通知的密钥,因此您必须添加一个告诉RestKit的关系:

  1. 对象中的源键
  2. JSON中的目标键
  3. 要为源密钥找到的内容使用的映射

  4. 根据您的添加内容,您需要进一步调查。添加以下代码:

    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
    

    会产生大量的日志记录。在发布时检查日志,它应该详细显示映射正在做什么。它对tasks有什么看法?

    另外,在发布之前调试list。它真的有任何任务吗?确定。