【问题标题】:RestKit one to one relationship mapping errorRestKit 一对一关系映射错误
【发布时间】:2015-03-03 18:48:16
【问题描述】:

我正在我的应用程序中使用 Restkit。我在映射一对一关系时遇到问题。

我有两个实体 Task 和 TaskNote。以前 Task 和 TaskNote 之间存在一对多的关系,即一个任务可以有多个笔记。

这段代码当时运行良好

    NSDictionary *taskNoteObjectMapping = @{
                                        @"noteID" : @"noteID",
                                        @"taskID" : @"taskID",
                                        @"time_stamp" : @"time_stamp",
                                        @"noteText" : @"noteText",
                                        @"note_user_phone_no" : @"note_user_phone_no",
                                        @"note_username" : @"note_username"
                                        };

RKEntityMapping *taskNoteEntityMapping = [RKEntityMapping mappingForEntityForName:@"TaskNote" inManagedObjectStore:managedObjectStore];
[taskNoteEntityMapping addAttributeMappingsFromDictionary:taskNoteObjectMapping];
taskNoteEntityMapping.identificationAttributes = @[@"noteID"];


[taskEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"taskNote" toKeyPath:@"taskNote" withMapping:taskNoteEntityMapping]];

但是由于某些要求,我必须将任务与其注释之间的一对多关系更改为一对一,即一个任务可以有一个注释。在此更改之后,我的映射开始失败。即当我使用 NSLog 观察 Task 与其 Note 的关系时,结果为零。并且 RestKit 正在记录这个错误

    Failed transformation of value at keyPath 'taskNote' to representation of type 'TaskNote': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value '(
    "<TaskNote: 0x17833a60> (entity: TaskNote; id: 0x17833aa0 <x-coredata:///TaskNote/t33EC8AB0-1E62-4041-8B91-4B57BC0474F74> ; data: {\n    noteID = 113;\n    noteText = \"Note from server\";\n    \"note_user_phone_no\" = \"+923335729486\";\n    \"note_username\" = Myself;\n    task = nil;\n    taskID = 248;\n    \"time_stamp\" = 14130583;\n})"
)' to TaskNote: none of the 2 value transformers consulted were successful." UserInfo=0x166ba240 {detailedErrors=(
    "Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 \"The given value is not already an instance of 'TaskNote'\" UserInfo=0x16628750 {NSLocalizedDescription=The given value is not already an instance of 'TaskNote'}",
    "Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3000 \"Expected an `inputValue` of type `NSNull`, but got a `__NSArrayM`.\" UserInfo=0x166ba1e0 {NSLocalizedDescription=Expected an `inputValue` of type `NSNull`, but got a `__NSArrayM`.}"

要映射的 JSON 格式为

 "tasks":[  
     {  
        "taskID":248,
        "listID":388,
        "taskName":"Cure",
        "taskSyncStatus":1,
        "taskState":0,
        "task_latitude":0,
        "task_longitude":0,
        "taskLog":[  ],
        "taskComment":[  ],
        "taskNote":[  
           {  
              "noteID":113,
              "taskID":248,
              "noteText":"Note from server",
              "note_user_phone_no":"+10001234567",
              "note_username":"Myself",
              "time_stamp":14130583
           }
        ]
     }

请指导我在这里做错了什么。如何将一个映射到关系。更改模型后,我还生成了实体 Task 和 TaskNote 的新子类。

如果需要,我可以提供更多信息。

【问题讨论】:

  • 现在.h 文件和xcdatamodeltaskNote 字段的类型/类是什么?
  • @AdilSoomro taskNote 目标实体是 xcdatamodel 中的 TaskNote。和 Task.h 相同的是 TaskNote

标签: ios iphone restkit restkit-0.20 ios7.1


【解决方案1】:

您发布的 JSON 将 taskNote 作为数组,这意味着您的 api 没有更改其结构。它仍然返回包含一个对象的相同数组。所以你有两个选择:

  1. 推荐:将taskNote 从 api 转换为对象。在这种情况下,您将获得不带方括号的 taskNote []。这意味着它不会是一个数组。
  2. 在 xcdatamodel 和 .h 文件中将 taskNote 作为数组,映射 taskNote 就像您已经在做的那样,并添加一个辅助方法 将数组的第一项作为任务说明返回给您。

查看Wain 的关于仅在此处映射第一个对象的答案:RestKit 0.22 : how to map only firstObject of an array

这是另一个关于谷歌群组的帖子,标题为"map first element from JSON array to single value using keypath mapping",回答如下:

键值编码无法做到这一点。您将需要映射 整个数组,然后访问第一个元素(可能通过 方便的方法)

【讨论】:

  • 谢谢,我自己在想方案#2,已经实现了#2,我会单独测试方案1,然后接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多