I'm having an issue very similar to this one
Basically I'm using Parse.com to load some objects, which have PFUser pointers, and then I'm also using includeKey to include those PFUsers, here's the code...
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]);
}
}];
Now for some reason, everynow and again, maybe 1 time out of 10, the game is crashing, with this error
Key "username" has no data. Call fetchIfNeeded before getting its value.
Although I can't tell if it's crashing when I try to use "username" above, or a little later when I try to use "username" but either way, I don't get why most of the time it has no problem including those extra objects and then a few times it doesn't. Any ideas?
It sounds as though the associated user
is not being loaded, but may be cached in most instances (and thus available). I can think of two ways to resolve this.
1) Call fetchIfNeeded
as suggested by the error message:
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) Specify pfUser.username
in the includeKey:
call
[query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
[query includeKey:@"pfUser.username"];
query.limit = 100;
...
This should tell Parse to load username
when the query is executed.
I had this same problem.
Here is what I did:
ex.
[someObject.user fetchIfNeeded]
this will get the information about the user that you need.
//if you run this code in cellForRowAtIndexPath and want to update the profile image for the user you can do the following.
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);
}
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With