Late Loading of remote Data (RestKit and CoreData)

Go To StackoverFlow.com

0

I'm working a long time on this problem, but couldn't find a solution that fits my needs.

The problem is, how could I load data and map them to the relationship without loading the whole structure

Simple example:

We got some birds:

{ 
    "birds":[{ 
            "bird":{"id":"1","value":"LaLeLu"}, 
            "bird":{"id":"2","value":"LeLeLa"}, 
            ... 
    }] 
} 

This could get mapped by something like this:

RKManagedObjectMapping *birdMapping = [RKManagedObjectMapping 
mappingForClass:[Bird class]]; 
menuMapping.primaryKeyAttribute = @"identifier"; 
[menuMapping mapKeyPath:@"value" toAttribute:@"value"]; 
[menuMapping mapKeyPath:@"id" toAttribute:@"identifier"]; 

[[[RKObjectManager sharedManager] mappingProvider] 
setMappingForKeyPath:"birds.bird"]; 

Works great by now.

Now every bird could have a lot of comments - but I don't wanne load all these comments with the first request.

Comments should get loaded when users clicks the specific bird.

So I request:

NSString *resourcePath = [NSString stringWithFormat:@"/birds/%@/ 
comments", myBird.id] 
[[RKObjectManager sharedManager] 
loadObjectsAtResourcePath:resourcePath]; 

I could change the response that it fits the needs of RestKit - but what are the needs?

{ 
    "comments":[{ 
            "comment"{"id":"1","value":"Comment1","bird_id":"1"} 
    }] 
} 

And now I don't have an idea how to map this response.

Mapping the comments without any relation to the birds is no problem:

RKManagedObjectMapping *commentMapping = [RKManagedObjectMapping 
mappingForClass:[Comment class]]; 
menuMapping.primaryKeyAttribute = @"identifier"; 
[menuMapping mapKeyPath:@"value" toAttribute:@"value"]; 
[menuMapping mapKeyPath:@"id" toAttribute:@"identifier"]; 

[[[RKObjectManager sharedManager] mappingProvider] 
setMappingForKeyPath:"comments.comment"]; 

Hope someone understands my problem and could help

2012-04-04 17:37
by PascalTurbo


0

For all who ware interested in the solution:

RestKit 0.10.0 fix the problem:

RKManagedObjectMapping *commentMapping = [RKManagedObjectMapping 
mappingForClass:[Comment class]]; 
commentMapping.primaryKeyAttribute = @"identifier"; 
[commentMapping mapKeyPath:@"value" toAttribute:@"value"]; 
[commentMapping mapKeyPath:@"id" toAttribute:@"identifier"];

// Here starts the relevant part:

[commentMapping mapKeyPath:@"bird_id" to Attribute:@"bird_id"];
[commentMapping mapRelationship:@"bird" withMapping:birdMapping];
[commentMapping connectRelationship:@"bird" withObjectPropertyForPrimaryKeyAttribute:"bird_id"]



[[[RKObjectManager sharedManager] mappingProvider] 
    setMapping:commentMapping ForKeyPath:"comments.comment"]; 
2012-04-06 14:06
by PascalTurbo
Ads