我的 CoreData 支持的实体中的一对多关系存在问题。
我的 JSON 看起来像这样:
[
{
"uuid": "0",
"title": "humor",
"providers": [{
"provider": {
"uuid": "1",
"title": "dilbert",
"sections": [{
"section": { "uuid":"1990", "title": "daily"},
"section": { "uuid":"1991", "title": "weekly"},
}],
},
"provider": {
"uuid": "2",
"title": "wizard of id",
},
"provider": {
"uuid": "3",
"title": "bc",
},
},],
},
{
"uuid": "22",
"title": "photo",
},
]
在我的例子中,顶级对象是一个类别,它可以有 0..* 提供者,它可以有 0..* 部分。
更新:总结一下,这个问题的根源是 JSON 格式错误。字典中不应有尾随逗号。
我的 RestKit 映射是:
RKEntityMapping *sectionMapping = [RKEntityMapping mappingForEntityForName:@"DHSection" inManagedObjectStore:managedObjectStore];
[sectionMapping addAttributeMappingsFromDictionary:@{
@"section.uuid" : @"uuid",
@"section.title" : @"title",
}];
sectionMapping.identificationAttributes = @[@"uuid"];
RKEntityMapping *providerMapping = [RKEntityMapping mappingForEntityForName:@"DHProvider" inManagedObjectStore:managedObjectStore];
[providerMapping addAttributeMappingsFromDictionary:@{
@"provider.uuid" : @"uuid",
@"provider.title" : @"title",
}];
providerMapping.identificationAttributes = @[@"uuid"];
RKRelationshipMapping *sectionRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"provider.sections" toKeyPath:@"sections" withMapping:sectionMapping];
[sectionRelationship setAssignmentPolicy:RKUnionAssignmentPolicy];
[providerMapping addPropertyMapping:sectionRelationship];
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"DHCategory" inManagedObjectStore:managedObjectStore];
[categoryMapping addAttributeMappingsFromDictionary:@{
@"uuid" : @"uuid",
@"title" : @"title",
}];
categoryMapping.identificationAttributes = @[@"uuid"];
RKRelationshipMapping *providerRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"providers" toKeyPath:@"providers" withMapping:providerMapping];
[providerRelationship setAssignmentPolicy:RKUnionAssignmentPolicy];
[categoryMapping addPropertyMapping:providerRelationship];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping method:RKRequestMethodGET pathPattern:@"category/list.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"category/list.json"];
NSDictionary *argsDict = nil;
BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
if (match) {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"DHCategory"];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"uuid" ascending:YES]];
return fetchRequest;
}
return nil;
}];
当 RestKit 解析响应时,它正确解析了两个类别,但它只加载了第一个提供者和部分。
2013 年 11 月 1 日更新:上面的更新映射更正了从关键路径“provider.sections”与“sections”映射的部分的关系映射。
问题可能源于响应与所映射的内容之间的差异。
2013-11-01 10:40:06.670 xyz[58006:1003] D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: (
{
providers = (
{
provider = {
sections = (
{
section = {
title = daily;
uuid = 1990;
};
}
);
title = dilbert;
uuid = 1;
};
}
);
title = humor;
uuid = 0;
},
...
而回应是
response.body=[
{
"uuid": "0",
"title": "humor",
"providers": [
{
"provider": {
"uuid": "1",
"title": "dilbert",
"sections": [{
"section": { "uuid":"1990", "title": "daily"},
"section": { "uuid":"1991", "title": "weekly"},
}
],
},
"provider": {
"uuid": "3",
"title": "wizard of id",
},
"provider": {
"uuid": "2",
"title": "bc",
},
},
],
},
...
有关如何修复或调试此问题的任何想法?
谢谢,
麦克风