gpt4 book ai didi

ios - 使用 RestKIt 在核心数据实体中映射 json 对象

转载 作者:行者123 更新时间:2023-11-29 12:54:25 25 4
gpt4 key购买 nike

我正在使用 RestKit 开发一些待办事项列表应用程序。我面临一些关于在 iOS 中使用 CoreData 返回的 json 表单服务器的映射问题。

场景

以下是我正在使用的列表和任务实体的图像。

enter image description here

当有人将新任务添加到服务器上的列表时,我返回该特定列表的 Json:

`GET www.mydomain.com\api\list\1`

[ { "list" :

{ "listID" : "1",
"listName" : "New List ",
"listSyncStatus" : "1"
},
"tasks" : [ { "listID" : "1",
"taskCompletionStatus" : "1",
"taskID" : "24",
"taskName" : "Server Added 2",
"taskSyncStatus" : "1"
},
{ "listID" : "1",
"taskCompletionStatus" : "1",
"taskID" : "25",
"taskName" : "Server Added 3",
"taskSyncStatus" : "1"
},
{ "listID" : "1",
"taskCompletionStatus" : "1",
"taskID" : "23",
"taskName" : "Server Added",
"taskSyncStatus" : "1"
}
]
}
]

我对上述 json 的响应描述符如下:

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

对于我的核心数据映射,我使用以下关系映射:

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

我的taskEntityMapping如下:

    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" ];

和我的listEntityMapping如下:

    //ALL LISTS RELATED REQUESTS
NSDictionary *listObjectMapping = @{
@"listID" : @"listID",
@"listName" : @"listName",
@"listSyncStatus" : @"listSyncStatus",

};

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

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

执行时我的应用程序崩溃并出现以下错误由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“预期字典表示”

请求日志如下:

2014-02-03 13:17:29.789 RKGist[984:70b] T restkit.network:RKObjectRequestOperation.m:178 GET 'http://mydomain.com/api/list/1':
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";
"User-Agent" = "RKGist/1.0 (iPhone Simulator; iOS 7.0.3; Scale/2.00)";
}
request.body=(null)
2014-02-03 13:17:30.670 RKGist[984:f03] T restkit.network:RKResponseMapperOperation.m:451 Mapping HTTP response to nil target object...
2014-02-03 13:17:30.670 RKGist[984:f03] D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: (
{
list = {
listID = 1;
listName = "New List ";
listSyncStatus = 1;
};
tasks = (
{
listID = 1;
taskCompletionStatus = 1;
taskID = 24;
taskName = "Server Added 2";
taskSyncStatus = 1;
},
{
listID = 1;
taskCompletionStatus = 1;
taskID = 25;
taskName = "Server Added 3";
taskSyncStatus = 1;
},
{
listID = 1;
taskCompletionStatus = 1;
taskID = 23;
taskName = "Server Added";
taskSyncStatus = 1;
}
);
}
)
and targetObject: (null)
2014-02-03 13:17:30.671 RKGist[984:f03] T restkit.object_mapping:RKMapperOperation.m:320 Examining keyPath 'tasks' for mappable content...
2014-02-03 13:17:30.671 RKGist[984:f03] D restkit.object_mapping:RKMapperOperation.m:297 Found mappable collection at keyPath 'tasks': (
(
{
listID = 1;
taskCompletionStatus = 1;
taskID = 24;
taskName = "Server Added 2";
taskSyncStatus = 1;
},
{
listID = 1;
taskCompletionStatus = 1;
taskID = 25;
taskName = "Server Added 3";
taskSyncStatus = 1;
},
{
listID = 1;
taskCompletionStatus = 1;
taskID = 23;
taskName = "Server Added";
taskSyncStatus = 1;
}
)
)

日志显示在键路径 tasks 找到可映射集合。

任何人都可以指导我我在这里做错了什么。是我返回的json格式错误还是我的映射代码错误?

最佳答案

您的响应描述符使用 keyPath:@"tasks"。这有效地“挖掘”了响应并忽略了 tasks 部分之外的任何内容。这意味着在映射期间您无权访问 list 部分。

addPropertyMapping: 与关系一起使用是为了嵌套映射 - 这不是您所拥有的。您实际上有 2 个单独的部分需要单独映射(因此,2 个响应描述符)以创建所有对象及其关联的 ID(以及连接对象的 ID)。然后,您需要使用外键映射来建立连接(使用 RKConnectionDescription )。

要检查的一件事是返回的 JSON 是什么。看起来好像它可能是 1 个包含对象的大数组,这没有意义。如果它是一个数组,那么您将需要改变您的整个方法。它应该是具有多个键的 1 个对象...

关于ios - 使用 RestKIt 在核心数据实体中映射 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21522655/

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