Restkit return 0 Objects in object Loader: didLoadObjects:

So, I'm trying to return an array of Leaderboard objects from the database using the rest of the calls. didLoadObjects returns 0 objects, although the mapping seems to be 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 display 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. A geo object consists of three fields, each of which must also be displayed. 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 didLoadObjects to correctly pass the objects back, but the Geo object of the Leaderboard still returns null. Thoughts?

+6
source share
1 answer

RESOLVED

The problem is with the display of the Geo object. If the object that restkit returns contains objects within it, you also need to provide mappings for these objects. 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; } 
+2
source

Source: https://habr.com/ru/post/900465/


All Articles