Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange issue with Parse.com with key not being included

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?

like image 728
Phil Avatar asked Aug 25 '13 13:08

Phil


2 Answers

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.

like image 150
alxmitch Avatar answered Nov 02 '22 23:11

alxmitch


I had this same problem.

Here is what I did:

  1. call fetchIfNeeded on the pointer.

ex.

[someObject.user fetchIfNeeded]

this will get the information about the user that you need.

  1. If you need a PFFile from the user object you can do the following.

//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);
        }


    }];
like image 28
Ronaldoh1 Avatar answered Nov 03 '22 00:11

Ronaldoh1