Hons
Hons

Reputation: 4046

RestKit recursive mapping

I have defined quite a simple mapping, having some plain properties, however now I run into the problem that my data structure on the Server is a tree, so I get a list of "CustomObject" which contains some properties and a list of "CustomObject" which ...

So in code it looks like this (simplified)

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

Which results obviously in an endless recursion.

Is there a smart way to do this mapping?

Upvotes: 2

Views: 781

Answers (1)

jmkk
jmkk

Reputation: 261

It's pretty simple and the recent RestKit library supports it just fine. Instead of recursively referencing +getCustomObjectMapping, you need to reference the mapping object you're constructing. Here's how you need to do it:

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

Upvotes: 7

Related Questions