2
votes

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?

1
Do you have any control of the web server response structure? This is not typically how you transfer the graph structure. You might want to see how the Facebook Graph API does it (ie: You retrieve the nodes and connections separately). - Paul de Lange
You mean like retrieve the first level and maybe a flag or something that shows if there are sub-levels? - Hons
That is another way. A recursive web service call is completely unscalable and also is not RESTful. Again, look at Facebook. - Paul de Lange

1 Answers

7
votes

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