Question

I'm really stuck trying to do something that I think is simple.

I'm using Parse, I have 2 Classes, Users and Posts. Posts contain a pointer to the Users table. I am able to return the object from Posts in the form of user = PFUser:5ifTaddiOU\\. Instead of that, I'm trying to get the actual username of that pfuser which is Stan Brady.

Can anyone help on how I could retrieve the username of that object?

This is how I'm querying parse

- (void) retrieveFromParse{
    PFQuery *retrieveStories = [PFQuery queryWithClassName:@"Posts"];


    [retrieveStories findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
       NSLog(@"%@",objects);

        if (!error) {
            storyArray = [[NSArray alloc] initWithArray:objects];
        }
        [storyTableView reloadData];
    }];

}

Retrieval

PFObject *tempObject = [storyArray objectAtIndex:indexPath.row];
   cell.cellTitle.text = [tempObject objectForKey:@"user"];

My log shows the return value of user = PFUser:5ifTaddiOU";\n}. I'm looking for username = Stan Brady in the log . I've also tried cell.cellTitle.text = [tempObject objectForKey:@"user.username"]; but that returns null

Was it helpful?

Solution

By default, a PFQuery won't fetch the actual objects from a relationship, only a reference to them.

Try telling your query to include the user object:

PFQuery *retrieveStories = [PFQuery queryWithClassName:@"Posts"];
[retrieveStories includeKey:@"user"];

You will hopefully then be able to do:

cell.cellTitle.text = [[tempObject objectForKey:@"user"] objectForKey:@"username"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top