Parse: How do I query using PFRelation when I just have PFUser?

空扰寡人 提交于 2019-11-28 10:21:28

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/17331572/parse-how-do-i-query-using-pfrelation-when-i-just-have-pfuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!