RestKit一对多的关系

时间:2013-11-01 02:11:37

标签: ios core-data restkit restkit-0.20

我在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",
        },
    },
    ],
},
...

有关如何修复或调试此问题的任何想法?

谢谢,

麦克

0 个答案:

没有答案