简体   繁体   中英

Parse.com - Query for Relation in custom Class

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM