gpt4 book ai didi

ios - RestKit 一对一关系映射错误

转载 作者:行者123 更新时间:2023-11-28 21:49:33 24 4
gpt4 key购买 nike

我在我的应用程序中使用 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 观察任务与其注释的关系时,结果为零。 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 的新子类。

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

最佳答案

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

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

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

这是另一篇关于 google groups 的帖子,标题为 "map first element from JSON array to single value using keypath mapping" ,它的回答是这样的:

There is no way to do this with key-value coding. You will need to map the entire array and then access the first element (perhaps via a convenience method)

关于ios - RestKit 一对一关系映射错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28827889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com