Question

I'm trying to query a PFRelation of the current user of the app to load a table view showing all of the users friends. Each user is saved under a class called "User" when they first use the app and once they add a friend that friend is added to the column "friendsRelation" for the user. I'm using this code i got got from this link and nothing is returned from the query.

PFQuery *query = [PFQuery queryWithClassName:@"User"];
[query whereKey:@"friendsRelation" equalTo:[PFUser currentUser]];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"An error occurred fetching friends");
    }

    else {
        [self.friends addObjectsFromArray:objects];
        [self.tableView reloadData];
    }
}];
Was it helpful?

Solution

Try this:

PFRelation *relation = [[PFUser currentUser] relationForKey:@"friendsRelation"];
PFQuery *query = [relation query];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
     // results contains all friends of current user
}];

OTHER TIPS

You are not query the PFUser table, so you cannot use [query whereKey:@"friendsRelation" equalTo:[PFUser currentUser]]; to query the User table, since the friendsRelation does not contain any PFUser objects but User objects. Just get the User object which represent for current user and use it to query the relation.

To Query the Users table don't use

[PFQuery queryWithClassName:@"User"];

Use instead

[PFUser query];

Only use the first if you have created your own "User" table and at this point if you checked the Data-browser you will see two different tables both called "User".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top