RestKit 映射上的关系错误

Posted

技术标签:

【中文标题】RestKit 映射上的关系错误【英文标题】:Relationship fault on RestKit Mapping 【发布时间】:2015-09-16 08:47:30 【问题描述】:

我真的不明白为什么我会出错:/ 有人可以看看吗?

Json 文件:


"total_count": 3,
"incomplete_results": false,
"winners": [
    
        "windate": "2015-05-26 17:11:30.0",
        "gift": "foo",
        "email": "foo",
        "giftack": "foo",
        "lastname": "foo",
        "gender": "foo",
        "firstname": "foo",
        "updategiftdate":null
    
],
"nbWinners": 3

映射文件:

    //Winner List Mapping
RKEntityMapping *winnerListMapping =
    [RKEntityMapping mappingForEntityForName:@"WinnerList"
                        inManagedObjectStore:managedObjectStore];
winnerListMapping.identificationAttributes = @[@"nbWinners"];
[winnerListMapping addAttributeMappingsFromDictionary:@
                                    @"total_count" : @"totalCount",
                             @"incomplete_results" : @"incompleteResults",
                                      @"nbWinners" : @"nbWinners" ];

//Winner Mapping
RKEntityMapping *winnerMapping =
    [RKEntityMapping mappingForEntityForName:@"Winner"
                        inManagedObjectStore:managedObjectStore];
winnerMapping.identificationAttributes = @[@"winDate"];
[winnerMapping addAttributeMappingsFromArray:@[
                                            @"windate", @"gift", @"email", @"giftack", @"lastname", @"gender", @"firstname", @"updategiftdate"]];

//Link Winner to WinnerList
[winnerListMapping addPropertyMapping:
    [RKRelationshipMapping relationshipMappingFromKeyPath:@"winners"
                                                toKeyPath:@"winners"
                                              withMapping:winnerMapping]];

//Run Mapping
RKResponseDescriptor *winnerListResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:winnerListMapping
                                             method:RKRequestMethodGET
                                        pathPattern:@"example.json"
                                            keyPath:nil
                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
 ];
[objectManager addResponseDescriptor:winnerListResponseDescriptor];

数据模型:

最后:

2015-09-16 10:13:25.018 iDol Check[26031:954937] ????????????????????????????????????????????????
(
    "<WinnerList: 0x7f9d0167f4b0> (entity: WinnerList; id: 0xd0000000000c0000 <x-coredata://7E40E0FD-3677-45EA-9BD1-59F755C05BC5/WinnerList/p3> ; data: \n    incompleteResults = 0;\n    nbWinners = 3;\n    totalCount = 3;\n    winners = \"<relationship fault: 0x7f9d0167afa0 'winners'>\";\n)"
)

提前感谢您的帮助!

【问题讨论】:

【参考方案1】:

我打赌这是错误。

//Link Winner to WinnerList
[winnerListMapping addPropertyMapping:
    [RKRelationshipMapping relationshipMappingFromKeyPath:@"winners"
                                                toKeyPath:@"winners"
                                              withMapping:winnerMapping]];

特别是 toKeyPath:@"winners" 您的 Winner 模型没有 winners 属性。

【讨论】:

relationshipMappingFromKeyPath 是 WinnerList 属性,toKeyPath 是 json 属性,不是吗?目的,就是把WinnerMapping的所有内容(很多Winner对象)就是WinnerList对象。所以获胜者不需要获胜者属性 你是对的。我注意到一个无法识别的选择器错误“[Winner Winners]”,并认为是这样。我猜你已经解决了这个问题,如果不是赢家映射在我看来是错误的。也许这会导致解析错误。尝试使用与上述winnerListMapping相同的方式使用字典。 是的,我解决了这个问题。但这不是真正的问题;)我真的不明白为什么它不起作用,但我已经改变了这样做的方式,现在它正在起作用。谢谢:)

以上是关于RestKit 映射上的关系错误的主要内容,如果未能解决你的问题,请参考以下文章

RestKit .22:无法成功映射与嵌套 JSON 的关系

Restkit/Core Data 关系映射,实体到相同类型的实体(父/子)

Core Data + RestKit 映射多对多关系?

RestKit 与特定核心数据对象的关系连接,无需键映射

RestKit 直接 + 反向关系映射

RestKit 一对多关系逆映射