Restkit在objectLoader中返回0个对象:didLoadObjects:

时间:2011-10-31 19:46:48

标签: iphone ios restkit

所以我试图使用rest调用从数据库中取回一组Leaderboard对象。 didLoadObjects返回0个对象,即使映射看起来是正确的:

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

这是didLoadObjects方法

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

这是映射代码:

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

更新

我发现问题出在映射中的Geo对象上。地理对象由三个字段组成,每个字段也需要映射。以下是Geo对象的映射:

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

这会导致didLoadObjects正确传回对象,但Leaderboard对象的Geo对象仍然返回null。想法?

1 个答案:

答案 0 :(得分:2)

分辨

该问题与Geo对象的映射有关。如果restkit返回的对象包含其中的对象,则还需要为这些对象提供映射。这是工作代码:

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

这是地理对象:

+(RKObjectMapping*) getMapping{
    RKObjectMapping* geoMapping = [RKObjectMapping mappingForClass:[Geo class]];
    [geoMapping mapAttributes:@"lat", @"lng", @"place", nil];
    return geoMapping; 
}