RestKit递归映射

时间:2012-08-01 14:43:31

标签: ios restkit

我定义了一个非常简单的映射,有一些简单的属性,但是现在我遇到的问题是我的服务器上的数据结构是一棵树,所以我得到一个包含一些属性和列表的“CustomObject”列表“CustomObject”的......

所以在代码中它看起来像这样(简化)

+ (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;
}

这显然会导致无休止的递归。

有没有一种聪明的方法来进行这种映射?

1 个答案:

答案 0 :(得分: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;
}