0

I have a class named Circle with a relation named "members" to the _User class.

Class Circle:

Data of class Circle

Class _User:

Data of class _User

I'm trying to query all Circles that the current user belongs to (inside the "members" relations).

PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:@"members"];

PFQuery *query = [relation query];

[query findObjectsInBackgroundWithBlock:^(NSArray *PF_NULLABLE_S objects, NSError *PF_NULLABLE_S error){
    //objects size here is 0
    //error is nil
}];

The problem is that the NSArray is empty and no error is received into the block.

One solution I'm thinking of is creating an actual table to store this relation and have a Relation column in both Circle and _User, but I believe there should be a better way to do this.

Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74

1 Answers1

1

It doesn't appear that user has a members col. So, asking a user for its members relation is sure to fail. You want to query Circle...

PFQuery *query = [PFQuery queryWithClassName:@"Circle"];
[query whereKey:@"members" equalTo:user];
danh
  • 62,181
  • 10
  • 95
  • 136
  • This worked like a charm but I'm a bit confused. Then when should a query be made on a `PFRelation`? Also, I tried the code I have in the question but instead of `PFUser` I used `PFObject *circle = [PFObject objectWithClassName:@"Circle"]` then obtain the `PFRelation` and query on it. Also didn't work, now I understand why. Also, why is `[query whereKey:@"members" equalTo:user];` Working since the relation "members" is a list of users? Should be something like "members contains X" instead of "members equals X"? I'm just trying to understand this better as I might be missing any philosophy. thx – Miguel Ribeiro May 19 '15 at 08:36
  • 1
    They've overloaded the idea of equalTo for plural columns to mean containsAnElementEqualTo. After you fetch circle objects, you can use the members relation to actually go fetch the members. That's when to query on the relation. (An array of pointers -- if the group members count is small -- has an advantage here in that you can include members in the query and fetch them eagerly). – danh May 19 '15 at 13:54