Question

Here is an example of what I am trying to do. I have the current user as PFUser and on another class named Item I have a relation named "owners" which is a relation of PFUser. I want to query for all instances of Item which have the current user in the relation.

I see examples of the opposite way of querying, but I do not see how to get a relation and then filter it to a PFUser match on the relation. Here is one example.

https://www.parse.com/questions/inverse-relationship-support

PFObject teacher = ... // PFObject of class "Teacher" PFRelation studentsRelation = [teacher relationforKey:@"students"]; PFQuery *query = studentsRelation.query; [query findObjectsInBackground:...

For my example I would do this...

PFObject item = [PFObject objectWithClassName:@"Item"];
PFRelation relation = [parseObject relationforKey:@"owner"]; // Filter to PFUser? 
[query findObjectsInBackground:...

I can use whereKey, but what do I use? Do I match on objectId?

[query whereKey:@"objectId" equalTo:user.objectId];

I keep getting Error 102: a valid pointer is needed for RelatedTo operator. My guess is that since I am starting with an empty object it has no starting point. The example with Teacher must start with a populated instance. The only thing that I have that is populated is PFUser. There has to be a way to query for the Item instances and filter it on the "owners" relation that it owns to the current user. I cannot figure it out and I have not found an example of how to do this query.

This question is also on Parse.com: https://parse.com/questions/how-do-i-query-using-pfrelation-when-i-just-have-pfuser

Was it helpful?

Solution

You can use whereKey:equalTo: on a relation column and pass it a PFObject. This query will then return all Teacher objects which have this student in their "students" relation:

PFQuery *query = [PFQuery queryWithClassName:@"Teacher"]; [query whereKey:@"students" equalTo:student];

In this example, the student is a PFObject with a className that matches the relation in "students". If this is a relation of PFUsers and you're looking for the current user's "Teacher"s, you'd use:

PFQuery *query = [PFQuery queryWithClassName:@"Teacher"]; [query whereKey:@"students" equalTo:[PFUser currentUser]];

This answer also posted on Parse's community forums: https://parse.com/questions/how-do-i-query-using-pfrelation-when-i-just-have-pfuser

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top