Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com get PFUser from related PFObject

I'm saving a photo to Parse as a PFObject with the [PFUser currentUser] user ID as one of its keys.

I want to display the photo in a table view alongside details from that PFUser. But when I try to get the user - PFUser *user = [self.photo objectForKey:@"user"]; - and then try to access the user's display name, I get the following exception:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Key "profilePicture" has no data.  Call fetchIfNeeded before getting 
its value.'

Calling fetchIfNeeded crashes the table, though, and the AnyPic tutorial does exactly what I'm trying to do without using fetchIfNeeded. AnyPic just calls PFUser *user = [self.photo objectForKey:@"user"]; and everything works.

Am I missing a step somewhere? Thanks!

Here's the cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    static NSString *CellIdentifier = @"Cell";

    PhotoCellTest *cell = (PhotoCellTest *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[PhotoCellTest alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier descriptionText:nil userNameText:nil captionText:nil];
    }

    photo = object;
    PFUser *user = [self.photo objectForKey:@"user"];
    PFFile *profilePictureSmall = [user objectForKey:@"profilePicture"];
    cell.profilePicPlaceholder.file = profilePictureSmall;



    return cell;
}

Edit

Here's where I set the keys for the PFObject:

PFObject *photo = [PFObject objectWithClassName:@"Photo"];
    [photo setObject:[PFUser currentUser] forKey:@"user"];
    [photo setObject:self.photoFile forKey:@"image"];
    [photo setObject:[nameField text] forKey:@"itemCaption"];

And here's where I set the keys for the PFUser:

PFFile *profilePic = [PFFile fileWithData:data];
    [[PFUser currentUser] setObject:profilePic forKey:@"profilePicture"];
    [[PFUser currentUser] saveInBackground];

When I NSLog user, I only get <PFUser:nOpK5splEX:(null)> { }, but when I NSLog [PFUser currentUser], I get <PFUser:nOpK5splEX:(null)> { displayName = "XXX"; facebookId = XXX; profilePicture = "<PFFile: XXX>"; username = XXX;}... So obviously the PFUser isn't getting set correctly.

Am I supposed to do something in the database to link the user fields or something like that?

like image 979
bmueller Avatar asked Sep 19 '12 00:09

bmueller


2 Answers

Got it figured out - I wasn't using an includeKey in the PFQuery.

like image 145
bmueller Avatar answered Nov 04 '22 02:11

bmueller


After

PFUser *user = [self.photo objectForKey:@"user"];

you should call

[user fetchIfNeeded];

like image 42
Mohsen Ebrahimzadeh Avatar answered Nov 04 '22 03:11

Mohsen Ebrahimzadeh