【发布时间】:2016-02-18 13:58:23
【问题描述】:
我有一个 Json 文件,其结构大致如下:
{
"id": "1",
"child1": {
"p1": "v1",
"p2": "v2"
},
"child2": {
"p1": "v1",
"p2": "v2"
}
}
child1 和 child2 属性通过关系映射进行映射,并且都使用相同的 RKEntityMapping。为了让 restkit 能够正确地做到唯一性,我想要一个额外的字段 type ,然后它的值将是 child1 或 child2 取决于它是哪个项目。我想使用 @metadata.mapping.rootKeyPath,但总是为零。
我的关系映射:
RKEntityMapping *childMapping = [RKEntityMapping mappingForEntityForName:@"Child" inManagedObjectStore:managedObjectStore];
[childMapping addAttributeMappingsFromDictionary:@{@"@parent.id" : @"userID", @"@metadata.mapping.rootKeyPath" : @"type"}];
[childMapping addAttributeMappingsFromArray:@[@"p1", @"p2"]];
childMapping.identificationAttributes = @[@"userID", @"type"];
[entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"child1"
toKeyPath:@"child1"
withMapping: childMapping]];
[entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"child2"
toKeyPath:@"child2"
withMapping: childMapping]];
有没有办法在关系映射中获取 rootKeyPath,以便我可以将其用作标识属性?或者也许是另一种解决方法来实现这一目标?
【问题讨论】:
-
子对象没有来自服务器的唯一 ID 是否有原因?
-
它们永远不会独立于主对象。所以他们的唯一密钥并不是真正必要的。
标签: ios objective-c restkit