gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:58:34 25 4
gpt4 key购买 nike

因此,我尝试使用 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。想法?

最佳答案

已解决

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

关于iphone - Restkit 在 objectLoader : didLoadObjects: 中返回 0 个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7958880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com