Trying to query a PFRelation for a specific user in Parse

1.9k views Asked by At

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];
    }
}];
3

There are 3 answers

0
Eric Qian On BEST ANSWER

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
}];
1
Eric Qian On

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.

0
Mohamed Saleh On

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".