鉴于以下核心数据关系:
Recipe <<---> Cookbook
Recipe <<---> Author
Cookbook <<---> Author
这个代表食谱的源JSON:
{
"id": 123,
"name": "Recipe Name",
"author": {
"id": 456,
"name": "Author Name"
},
"cookbook": {
"id": 789,
"title": "Cookbook Title"
}
}
我正在寻找一个RestKit映射,它将产生以下条件的配方:
recipe.cookbook IN recipe.author.cookbooks
recipe.cookbook.author === recipe.author
这是我到目前为止所拥有的:
recipeMapping =
[RKEntityMapping mappingForEntityForName:@"Recipe" inManagedObjectStore:_store];
[recipeMapping addAttributeMappingsFromDictionary:@{
@"id": @"id",
@"name": @"name"
}];
recipeMapping.identificationAttributes = @[@"id"];
RKEntityMapping *authorMapping =
[RKEntityMapping mappingForEntityForName:@"Author" inManagedObjectStore:_store];
[authorMapping addAttributeMappingsFromDictionary:@{
@"id": @"id",
@"name": @"name"
}];
authorMapping.identificationAttributes = @[@"id"];
RKEntityMapping *cookbookMapping =
[RKEntityMapping mappingForEntityForName:@"Cookbook" inManagedObjectStore:_store];
[cookbookMapping addAttributeMappingsFromDictionary:@{
@"id": @"id",
@"title": @"title"
}];
cookbookMapping.identificationAttributes = @[@"id"];
[recipeMapping
addRelationshipMappingWithSourceKeyPath:@"cookbook"
mapping:cookbookMapping];
[recipeMapping
addRelationshipMappingWithSourceKeyPath:@"author"
mapping:authorMapping];
// Now what?
应用此映射后,recipe.cookbook
是Cookbook
的正确实例,recipe.author
是Author
的正确实例,但当然recipe.cookbook.author
是nil和recipe.author.cookbooks
是空的。
答案 0 :(得分:0)
由于您没有所需信息,因此无法在映射时执行此操作。
您可能能够设置使用外键的关系,而不是使用外键的特殊属性而是使用3个对象之间的关系。我会说这会有点脆弱,因为它依赖于映射过程中处理关系的顺序。
相反,我会在后处理期间(即在映射完成后的回调中或通过在托管对象子类上实现willSave
)或在您实际需要知道时按需执行此操作关系。这可以使用KVC完成,例如:
如果您执行获取以获取所有作者,则可以使用以下方法为每个作者构建食谱:
author.cookbooks = [author.recipes valueForKeyPath:@"@distinctUnionOfObjects.cookbooks"];