0

我想將連接添加到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 - 這已經在這裏回答了,但解決方案看起來很奇怪。這是做到這一點的唯一方法嗎?

回答

1

而不必爲child添加一個child_id屬性(一些人聲稱他們這樣做了,但它看起來不對我)。

這實際上是正確的方式來做到這一點...

點是RestKit需要一些方法來這兩個項目映射到一起,一些關鍵的與搜索和查找的項目連接。這些信息只需要是暫時的(假設您一次加載數據或者識別的另一側是持久屬性)。沒有其他方法可以做到。

+0

在我的情況下,我在不同的請求中加載數據。我期望RestKit在獲取正確的ManagedObject模型時臨時保存'id'屬性,然後擺脫它 - 而不必映射'id'屬性。 – Indigon