5
votes

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?

2
can you post the code where you access the user data? maybe post the whole cellForRowAtIndexPath method if you do it there.danh
Can you show how you are setting the key on the PFObject?danielbeard
I agree with @danielbeard. What leads you to believe there's a value for the key profilePicture? Also, what kind of object is cell. profilePicPlaceholder?danh
See edits above. The NSLog for user is blank - it's getting the user ID, but none of its associated fields. When I NSLog currentUser, all the associated fields are populated...bmueller
cell.profilePicPlaceholder is a PFImageViewbmueller

2 Answers

8
votes

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

3
votes

After

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

you should call

[user fetchIfNeeded];