我遇到的问题与此one
非常相似基本上我正在使用Parse.com加载一些具有PFUser指针的对象,然后我也使用includeKey来包含那些PFUser,这里是代码......
PFQuery *query = [PFQuery queryWithClassName:@"GameVillageObject"];
[query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
[query includeKey:@"pfUser"];
query.limit = 100;
[sharedInstance requestSentWithDesc:@"Get all village objects in region"];
[query findObjectsInBackgroundWithBlock:^(NSArray *PUObjects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"Successfully retrieved %d village objects from server.", PUObjects.count);
if(PUObjects.count > 0)
{
villageObjects = PUObjects;
for (int i=0; i<[villageObjects count]; i++)
{
PFObject *villageItem = [villageObjects objectAtIndex:i];
PFUser *user = [villageItem objectForKey:@"pfUser"];
NSString *userName = [NSString stringWithFormat:@"%@",[user objectForKey:@"username"]];
NSLog(@"User name is: %@.", userName);
}
[self setupVillageList];
[sharedInstance centerImage:marketItemsContainer xChoice:YES yChoice:NO];
}
} else {
// Log details of the failure
NSLog(@"Getting village objects Error: %@ %@", error, [error userInfo]);
}
}];
现在出于某种原因,每次出现,也许再次,也许是10次中的1次,游戏崩溃,出现此错误
密钥“用户名”没有数据。在获取之前调用fetchIfNeeded 值。
虽然我无法判断当我尝试使用上面的“用户名”时它是否会崩溃,或者稍后我尝试使用“用户名”时,无论哪种方式,我都不知道为什么大部分时间它都有没有问题,包括那些额外的对象,然后几次没有。有什么想法吗?
答案 0 :(得分:13)
听起来好像没有加载关联的user
,但是在大多数情况下可以缓存(因此可用)。我可以想出两种解决方法。
1)按照错误消息的建议调用fetchIfNeeded
:
PFQuery *query = [PFQuery queryWithClassName:@"GameVillageObject"];
[query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
[query includeKey:@"pfUser"];
...
PFObject *villageItem = [villageObjects objectAtIndex:i];
PFUser *user = [villageItem objectForKey:@"pfUser"];
[user fetchIfNeeded];
NSString *userName = [NSString stringWithFormat:@"%@",[user objectForKey:@"username"]];
NSLog(@"User name is: %@.", userName);
2)在pfUser.username
电话
includeKey:
[query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
[query includeKey:@"pfUser.username"];
query.limit = 100;
...
这应该告诉Parse在执行查询时加载username
。
答案 1 :(得分:1)
我有同样的问题。
这是我做的:
离。
[someObject.user fetchIfNeeded]
这将获得有关您需要的用户的信息。
//如果您在cellForRowAtIndexPath中运行此代码并想要更新用户的配置文件图像,则可以执行以下操作。
User *user = (User *)someObject.user;
[user.profileImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
cell.userProfileImage.image = image;
}else{
NSLog(@"Print error!!! %@", error.localizedDescription);
}
}];