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

Posted

技术标签:

【中文标题】Restkit 在 objectLoader 中返回 0 个对象:didLoadObjects:【英文标题】:Restkit returning 0 Objects in objectLoader: didLoadObjects: 【发布时间】:2011-10-31 19:46:48 【问题描述】:

所以我试图使用休息调用从数据库中取回一组排行榜对象。即使映射看起来正确,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。想法?

【问题讨论】:

嗨,很高兴您找到了解决方案。请提供解决方案作为您的答案,并且可以从实际问题中删除。 感谢@iDroid 的提示,我继续这样做了。 【参考方案1】:

已解决

问题在于 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; 

【讨论】:

以上是关于Restkit 在 objectLoader 中返回 0 个对象:didLoadObjects:的主要内容,如果未能解决你的问题,请参考以下文章

使用 Three.js ObjectLoader 正确渲染动画

我所有控制器的“唯一/继承”RKObjectLoaderDelegate

在 QAbstractItemModel 中返​​回正数行和零列是不是可以?

试图在资源 Laravel 中返​​回数据透视表

如何在模型 Laravel 中返​​回多个自定义值

如何在 Laravel 中返​​回数据库表名