0

我在使用具有相同 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];

但是,如果我想使用相同的 toKeyPath(“prop_3”)将另一个 RKRelationshipMapping 添加到“someObjectRequestMapping”,则 RestKit 失败并出现错误:

*'NSInternalInconsistencyException',原因:'无法为 keyPath 邀请属性添加映射,一个已经存在...'*

带有两个失败的 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。

任何帮助将不胜感激。

最好的祝福,

达米尔

4

1 回答 1

0

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

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

于 2013-09-11T14:35:45.743 回答