2012-08-01 160 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; 
}