多个RKRelationshipMapping使用相同的toKeyPath

时间:2013-09-11 13:54:05

标签: ios mapping restkit

使用多个带有相同toKeyPath(destinationKeyPath)的RKRelationshipMappings时遇到问题。 我已成功使用一个RKRelationshipMapping到一个toKeyPath但我无法使用相同的toKeyPath附加第二个RKRelationshipMappings。

一个有效的RKRelationshipMapping代码:

RKObjectMapping *someObjectRequestMapping = [RKObjectMapping requestMapping];
[someObjectRequestMapping addAttributeMappingsFromDictionary:@{@"prop1" : @"prop_1", @"prop2" : @"prop_2"}]; 

RKObjectMapping *firstSubObjectMapping = [RKObjectMapping requestMapping];
[firstSubObjectMapping addAttributeMappingsFromDictionary:@{@"subProp1" : @"sub_prop1", @"subProp2" : @"sub_prop2"}];

[someObjectRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"prop3"
                                                                                         toKeyPath:@"prop_3"
                                                                                       withMapping:firstSubObjectMapping];

但是,如果我想将另一个RKRelationshipMapping添加到具有相同toKeyPath(“prop_3”)的“someObjectRequestMapping”,则RestKit会失败并显示错误:

*'NSInternalInconsistencyException',原因:'无法为keyPath invites_attributes添加映射,已经存在...'*

包含两个失败的RKRelationshipMappings的代码:

RKObjectMapping *someObjectRequestMapping = [RKObjectMapping requestMapping];
[someObjectRequestMapping addAttributeMappingsFromDictionary:@{@"prop1" : @"prop_1", @"prop2" : @"prop_2"}]; 

RKObjectMapping *firstSubObjectMapping = [RKObjectMapping requestMapping];
[firstSubObjectMapping addAttributeMappingsFromDictionary:@{@"subProp1" : @"sub_prop1", @"subProp2" : @"sub_prop2"}];

RKObjectMapping *secondSubObjectMapping = [RKObjectMapping requestMapping];
[secondSubObjectMapping addAttributeMappingsFromDictionary:@{@"subProp2" : @"sub_prop2", @"subProp3" : @"sub_prop3"}];

[someObjectRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"prop3"
                                                                                         toKeyPath:@"prop_3"
                                                                                       withMapping:firstSubObjectMapping];

[someObjectRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"prop4"
                                                                                         toKeyPath:@"prop_3"
                                                                                       withMapping:secondSubObjectMapping];

映射用于RKRequestDescriptor。

非常感谢任何帮助。

最诚挚的问候,

达米尔

1 个答案:

答案 0 :(得分:0)

错误很明显,RestKit不会允许歧义,因此每个键路径只能有一个映射。

通常,如果这是一个响应描述符,您将使用动态映射来确定哪个关系映射是合适的。对于请求描述符,您可以自己执行类似的操作,分析要发送的对象并确定适合的映射。这意味着有2个不同的映射,并在它们之间手动选择。这也可以使用多个RKObjectManager来完成,每个{{1}}配置一组不同的映射。