我有以下核心數據模型是我的應用程序:RestKit:連接許多一對多的關係
+--------------------+ +-------------+
| Ingredient | | Dish |
+--------------------+ +-------------+
| id | | id |
| title | | title |
| dishId (transient) |
+--------------------+ +-------------+
| dishes | <<--->> | ingredients |
+--------------------+ +-------------+
我也有一個Web服務,它允許我請求所有的成分對於給定的菜樣這個:
/dish/1/ingredients
(其中1
是一些碟ID)。
這樣的響應請求如下:
{
{ "id": "1", "title": "milk" },
{ "id": "2", "title": "egg" },
{ "id": "3", "title": "flour" }
}
我讀過有關RKConnectionDescription,但我無法弄清楚如何使用它來這兩個實體在這種特殊情況下連接。
這裏的映射到目前爲止我有:
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Ingredient" inManagedObjectStore:store];
[mapping setIdentificationAttributes:@[@"id"]];
[mapping addAttributeMappingsFromDictionary:@{
@"id" : @"id",
@"title" : @"title",
@"@metadata.dishId" : @"dishId"
}];
UPDATE
我添加了一個新的瞬態特性dishId
我Ingredient
實體,並試圖建立這樣的連接:
NSEntityDescription *entityDescription =
[NSEntityDescription entityForName:@"Ingredient" inManagedObjectContext:mainContext];
NSRelationshipDescription *relationship =
[entityDescription relationshipsByName][@"dishes"];
RKConnectionDescription *connection =
[[RKConnectionDescription alloc] initWithRelationship:relationship attributes:@{
@"dishId" : @"id"
}];
[mapping addConnection:connection];
但每次我要求另一道菜的配料時,它會取代舊連接ns與新的,所以我基本上得到一對一的關係。例如,我首先要求煎蛋的配料,並將煎蛋與雞蛋,牛奶和麪粉連接起來。然後,我要求煎餅配料,它將煎餅與雞蛋,牛奶和麪粉連接起來,但煎蛋與這些配料的連接已經丟失。
assignmentPolicy
在RKConnectionDescription
上不可用。
我沒有問題從元數據獲取碟ID。當我每次請求只有一個dishId時,我不知道如何爲多對多關係設置「RKConnectionDescription」。 – HiveHicks
通常您想要將新數據與現有數據合併(而不是替換它)。將關係映射設置爲使用'RKUnionAssignmentPolicy'這樣做,但我不能說我需要使用外部映射來完成它,所以我需要稍後再進行檢查。我猜你看到了連接,但其他(現有)連接斷開了? – Wain
是的,確切地說。請看這個問題的更新。 – HiveHicks