我有以下JSON结构,我想在CoreData中使用RestKit进行映射:
[
{
"id": 1,
"type": "folder",
"name": "Folder 1",
"children": [
{
"id": 1,
"type": "document",
"name": "Document 1"
},
{
"id": 2,
"type": "folder",
"name": "Folder 2",
"children": [
{
"id" : 2,
"type": "document",
"name": "Document 2"
}
]
}
]
}
]
它是典型的文件系统结构,其中文件夹可以包含其他文件夹或文档。
我有两个CoreData实体:文件夹和文档。 Folder实体具有以下关系:
这是我的映射代码:
RKEntityMapping* foldersMapping = [RKEntityMapping mappingForEntityForName:@"Folder" inManagedObjectStore:managedObjectStore];
foldersMapping.identificationAttributes = @[ @"objectId" ];
[foldersMapping addAttributeMappingsFromDictionary:@{@"id" : @"objectId",
@"name" : @"name"}];
RKEntityMapping* documentsMapping = [RKEntityMapping mappingForEntityForName:@"Document" inManagedObjectStore:managedObjectStore];
documentsMapping.identificationAttributes = @[ @"objectId" ];
[documentsMapping addAttributeMappingsFromDictionary:@{@"id" : @"objectId",
@"name" : @"name"}];
在我的动态映射中,我使用对象的类型来决定它是文档还是文件夹。
RKDynamicMapping* dynamicMapping = [RKDynamicMapping new];
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
if ([[representation valueForKey:@"type"] isEqualToString:@"Folder"]) {
return foldersMapping;
} else if ([[representation valueForKey:@"type"] isEqualToString:@"Document"]) {
return documentsMapping;
}
return nil;
}];
现在我将动态映射添加为关系映射到属性子项的文件夹映射两次。
[foldersMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"children" toKeyPath:@"children" withMapping:dynamicMapping]];
[foldersMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"children" toKeyPath:@"documents" withMapping:dynamicMapping]];
这是我的问题:这一切都有效,但我收到了以下警告:
RestKitNesterFolderTestProject [9969:3a03] W. restkit.object_mapping:RKMappingOperation.m:554警告:失败 映射嵌套对象:无法完成操作。 (可可 错误1550。)
我认为问题在于我将关系子项的映射加倍,因此RestKit尝试将每个子项映射两次,一次作为文件夹,一次作为文档。
感谢任何帮助。