2012-08-01 164 views
2

我已經定義了一個相當簡單的映射,有一些簡單的屬性,但現在我遇到了問題,我的服務器上的數據結構是一棵樹,所以我得到一個「CustomObject」列表,其中包含一些屬性和「CustomObject」的名單...RestKit遞歸映射

,這樣,代碼看起來像這樣(簡體)

+ (RKObjectMapping*)getCustomObjectMapping 
{ 
    RKObjectMapping* customObjectMapping = [RKObjectMapping mappingForClass:[CustomObject class]]; 
    [customObjectMapping mapKeyPath:@"title" toAttribute:@"title"]; 
    [..] 

    // Define the relationship mapping 
    //[customObjectMapping mapKeyPath:@"nextLevel" toRelationship:@"nexLevel" withMapping:[self getCustomObjectMapping]]; 

    return customObjectMapping; 
} 

其中在一個無限遞歸明顯地導致。

有沒有一種智能的方法來做這個映射?

+0

您是否有任何對Web服務器響應結構的控制?這通常不是您如何傳輸圖形結構的方式。你可能想看看Facebook Graph API如何實現它(即:你分別檢索節點和連接)。 – 2012-08-02 06:37:00

+0

你的意思是像檢索第一級,也許是一個標誌或東西,顯示是否有子級別? – Hons 2012-08-02 06:39:21

+0

這是另一種方式。遞歸Web服務調用是完全不可縮放的,也不是RESTful。再看看Facebook。 – 2012-08-02 06:41:38

回答

7

這很簡單,最近RestKit庫支持它就好了。 而不是遞歸引用+ getCustomObjectMapping,您需要引用您正在構建的映射對象。 以下是您需要這樣做的方法:

+ (RKObjectMapping*)getCustomObjectMapping 
{ 
    RKObjectMapping* customObjectMapping = [RKObjectMapping mappingForClass:[CustomObject class]]; 
    [customObjectMapping mapKeyPath:@"title" toAttribute:@"title"]; 
    [..] 

    // Define the relationship mapping 
    [customObjectMapping mapKeyPath:@"nextLevel" toRelationship:@"nexLevel" withMapping:customObjectMapping]; 

    return customObjectMapping; 
}