0

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 Answers 2

1

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.

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

Comments

0

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) {…}

3 Comments

Thanks. I got it working, just had to replace PFQuery *membersQuery = [PFQuery queryWithClassName:@"Members"]; with PFObject *currentGroup = [[PFUser currentUser] objectForKey:@"OwnerGroup"]; PFQuery *membersQuery = [PFUser query]; Is this the correct way of doing member search? It's not looking under "Members"(PFRelation key) in "Group" Class, just searching for matching keys in whole User Class. This makes Group class useless.
I don’t know if this is the best way to achieve your query, but i followed Parse example about relational query : parse.com/docs/ios_guide#queries-relational/iOS . The first example with posts and comments works this way. Your « groups » class is not useless, because it can still keeps information about the whole group without having to copy them from each user of the group. On the other hand, the « members » key is no longer necessary in your groups class.
Thanks. I'll do it this way. Have no idea how to pull list of members from "Member" key anyway.

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.