0
votes

I have 2 classes: PFUser and Groups. In "Groups" class I have all my groups and each group has a relation key called "Members". "Members" holds a list of users related to current group.

How do I query for "Members" key so it returns an array of users?

I've done similar query in past for User related relations, but then I just passed in PFRelation *friendsRelation = [PFUser currentUser] objectForKey:@"friendsRelation"] into query.

This time can't get it to work.

The closest I've come is :

PFQuery *query = [PFQuery queryWithClassName:@"Groups"]; [query whereKey:@"Members" equalTo:[PFUser currentUser]]; [query orderByAscending:@"username"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {…}

p.s. Each user has his group(PFObject) added as a key.

2

2 Answers

1
votes

How to do a PFRelation query for custom Class:

    //1. Get objectID for object from custom class. Previously added as a key for user.
PFObject *currentGroup = [[PFUser currentUser] objectForKey:@"Group"];

//2. Set relation key for which to do the query.
PFRelation *relation = [currentGroup relationForKey:@"Members"];

PFQuery *membersQuery = [relation query];

//Do the query!
[membersQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {}];

There's no need to define for which class to do the query. ObjectIDs are unique.

Hope this will help someone.

0
votes

If I understood your issue correctly, you want to have an array of « Members » in the same « Group » .

Your query is related to the class « Groups », it should be « Members » instead. You shouldn’t look for « members » equals to [PFUser currentUser], because it will always return only one item in the best case : the PFUser linked with your iPhone/iPad.

If your users has a group key (let’s name it « OwnerGroup » ), then you can try :

PFQuery *membersQuery = [PFQuery queryWithClassName:@"Members"];
[membersQuery whereKey:@"OwnerGroup" equalTo:currentGroup];
[membersQuery orderByAscending:@"username"];
[membersQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {…}