0
鑑於下列核心數據關係:RestKit - 與關係映射映射單個源向多個目的地
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
是零,並且recipe.author.cookbooks
是空的。
感謝您的建議!我現在正在後處理步驟中充實關係,但我必須相信在映射中有一種方法可以實現。我能做的就是將'recipe.cookbook'映射到'recipe.author.cookbooks'和'recipe.author'到'recipe.cookbook.author',但是我的映射不能滿足我原來的身份要求題;這兩本食譜和兩位作者是管理對象上下文中的不同實體。 – steveluscher
好的,你可以通過RestKit檢查具有現有id的實體並更新而不是創建新的實體。您需要在映射上設置'identificationAttributes'。 – Wain
我試過了;擡頭。 ⇧ – steveluscher