我想將連接添加到RestKit映射,但沒有成功。將一對一外鍵連接添加到RestKit映射時的問題
我的JSON如下:
{
parent: {
token: "token"
title: "title",
description: "description",
child_id: 1
}
}
我CoreData方案定義了Parent
ManagedObject具有child
密鑰(和相反)下一個Child
關係。在CoreData中已經有相應的Child
對象後,Parent
映射發生,所以這不是問題。
爲了解決這個問題,我跟蹤了一些關於網絡的鏈接和討論,但都沒有成功。什麼是正確的方式來做到這一點,而不必添加child_id
財產Parent
(有人聲稱他們這樣做,但它看起來不對我)。
假設我有這樣的映射:
- (RKEntityMapping *)parentResponseMapping {
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Parent" inManagedObjectStore:[self myStore]];
[mapping addAttributeMappingsFromArray:@[@"title", @"description"]];
mapping.identificationAttributes = @[@"token"];
return mapping;
}
起初,我嘗試添加這樣的連接:
NSEntityDescription *parentEntity = [NSEntityDescription entityForName:@"Parent" inManagedObjectContext:[self myContext]];
NSRelationshipDescription *childRelationship = [parentEntity relationshipsByName][@"seller"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:childRelationship keyPath:@"child_id"];
[mapping addConnection:connection];
但我得到一個錯誤,child_id
是不是一個有效的屬性(這是哪裏我試圖將child_id
屬性添加到我的方案中,但再一次,這對我來說似乎很混亂,而且它不起作用)。
我也試圖加入由連接:
[addConnectionForRelationship:@"child" connectedBy:@"child_id"]
但是,這也不能工作。
無論如何,這兩種方法都會忽略子映射。
什麼是對的方法來實現這個?
編輯:RestKit Object Mapping relationships with foreign keys - 這已經在這裏回答了,但解決方案看起來很奇怪。這是做到這一點的唯一方法嗎?
在我的情況下,我在不同的請求中加載數據。我期望RestKit在獲取正確的ManagedObject模型時臨時保存'id'屬性,然後擺脫它 - 而不必映射'id'屬性。 – Indigon