Ios RestKit一对多关系

Ios RestKit一对多关系,ios,core-data,restkit,restkit-0.20,Ios,Core Data,Restkit,Restkit 0.20,我的CoreData支持实体中的一对多关系有问题 我的JSON如下所示: [ { "uuid": "0", "title": "humor", "providers": [{ "provider": { "uuid": "1", "title": "dilbert", "sections": [{ "section

我的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",
        },
    },
    ],
},
...
关于如何修复或调试这个问题有什么想法吗

谢谢


迈克

代码看起来很合理。打开映射的跟踪日志记录,看看它能告诉你什么。@Wain,谢谢你的帮助。跟踪显示我的节映射不正确(应该是“provider.sections”vs“sections”)。它还显示映射的内容与响应不匹配。