5

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?

5
  • can you post the code where you access the user data? maybe post the whole cellForRowAtIndexPath method if you do it there. Commented Sep 19, 2012 at 0:09
  • Can you show how you are setting the key on the PFObject? Commented Sep 19, 2012 at 0:25
  • 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? Commented Sep 19, 2012 at 0:26
  • 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... Commented Sep 19, 2012 at 0:36
  • cell.profilePicPlaceholder is a PFImageView Commented Sep 19, 2012 at 0:37

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

i also have the same problem. can you help me?
3

After

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

you should call

[user fetchIfNeeded];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.