Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restkit returning 0 Objects in objectLoader: didLoadObjects:

So I am trying to get back an array of Leaderboard objects from a database using rest calls. didLoadObjects returns 0 objects even though the mapping seems correct:

 RKObjectManager *svc = [RKObjectManager sharedManager];
 NSString *resourcePath = leaderboardResourcePath;
 RKObjectMapping* mapping = [svc.mappingProvider objectMappingForClass:[Leaderboard class]];
 RKObjectLoader *loader = [[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath objectMapping:mapping delegate:self];

Here is the didLoadObjects method

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { 
    if ([objectLoader.resourcePath isEqualToString:leaderboardResourcePath])
    {
        @synchronized(standings)
        {
             standings = objects;
        }
    }
}    

Here is the mapping code:

+ (void)setMappings
{
    //Standings mapping
    RKObjectMapping* leaderboardMapping = [RKObjectMapping mappingForClass:[Leaderboard class]];
    [leaderboardMapping mapKeyPath:@"uname" toAttribute:@"uname"];
    [leaderboardMapping mapKeyPath:@"geo" toAttribute:@"geo"];
    [leaderboardMapping mapKeyPath:@"week" toAttribute:@"week"];
    [leaderboardMapping mapKeyPath:@"year" toAttribute:@"year"];
    [leaderboardMapping mapKeyPath:@"pts" toAttribute:@"pts"];  

    [[RKObjectManager sharedManager].mappingProvider addObjectMapping:leaderboardMapping];

    RKObjectRouter *router = [RKObjectManager sharedManager].router;

    // Define a default resource path for all unspecified HTTP verbs  
    [router routeClass:[Leaderboard class] toResourcePath:leaderboardResourcePath];  
}

UPDATE

I found out that the problem is with the Geo object in the mappings. The geo object consists of three fields each of which also need to be mapped. Here is the mapping for the Geo object:

+ (void)setMappings
{
    //Standings mapping
    RKObjectMapping* geoMapping = [RKObjectMapping mappingForClass:[Geo class]];
    [geoMapping mapKeyPath:@"lat" toAttribute:@"lat"];
    [geoMapping mapKeyPath:@"lng" toAttribute:@"lng"];
    [geoMapping mapKeyPath:@"place" toAttribute:@"place"];

    [[RKObjectManager sharedManager].mappingProvider addObjectMapping:geoMapping];
    [[RKObjectManager sharedManager].mappingProvider setMapping:geoMapping forKeyPath:@"geo"];   
}

+ (void)initialize{
    [super initialize];
    if ([self class] == [Geo class]) {
        [self setMappings];
    } 
}

This causes the didLoadObjects to correctly pass back the objects but the Geo object of the Leaderboard object still comes back null. Thoughts?

like image 747
Endama Avatar asked Oct 31 '11 19:10

Endama


1 Answers

RESOLVED

The issue lied with the mapping for the Geo object. If the object that restkit returns contains objects within it, you need to provide mappings for those objects as well. Here is the working code:

+ (void)setMappings
{
    RKObjectMapping* leaderboardMapping = [RKObjectMapping mappingForClass:[Leaderboard class]];
    [leaderboardMapping mapAttributes:@"uname", @"week", @"year",  @"pts", nil];

    // Define the relationship mapping
    [leaderboardMapping mapKeyPath:@"geo" toRelationship:@"geo" withMapping:[Geo getMapping]];

    [[RKObjectManager sharedManager].mappingProvider addObjectMapping:leaderboardMapping];
}

Here is the geo object:

+(RKObjectMapping*) getMapping{
    RKObjectMapping* geoMapping = [RKObjectMapping mappingForClass:[Geo class]];
    [geoMapping mapAttributes:@"lat", @"lng", @"place", nil];
    return geoMapping; 
}
like image 78
Endama Avatar answered Sep 30 '22 15:09

Endama