Reputation: 2972
This is my first question asked and hopefully someone can help me. I have looked around and nothing on this subject has helped me in my situation yet which is why I resorted to posting a request for help.
I have a parse.com database setup with the following Classes:
Users, Followers, and Items.
Followers contains two columns follower and following to keep track of which user is following which user.
Items contains an author column which is a pointer to a User object.
My goal is to make a query for a ParseQueryAdapter that will return Items objects. The path that I have to Items right now is to query the Followers class to get a list of users that the current user is following. Then use those user objects as pointers query the Items class for items the followed user has created.
Here is a diagram:
Followers
follower following
------- ---------
UserA UserB
UserB UserC
UserB UserA
Items
author column1 column2
------ ------- -------
UserA foo bar
UserA foo2 bar2
UserB blah blah
UserC blah blah
Based on the above database structure let's assume that currentUser is UserB. If UserB queries the Follower table he will retrieve his follower list (UserC and UserA) and then I would like to query to go out and retrieve all of the Items created by those that UserB follows (which should return 3 results based on the above structure). Based on my limited understanding of how parse.com works here is what I have tried:
public class ItemListItemAdapter extends ParseQueryAdapter<ParseObject>{
public ItemListItemAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
ParseUser currentUser = ParseUser.getCurrentUser();
ParseQuery<ParseObject> followerQuery = new ParseQuery("Followers");
followerQuery.whereEqualTo("follower", currentUser);
ParseQuery<ParseObject> itemQuery = new ParseQuery("Items");
itemQuery.whereMatchesQuery("author", followerQuery);
return itemQuery;
}
});
}
As I understand it I am first creating a query that will get the followers that currentUser is following. Then I should have a list of User objects that can be used to query the Items class where the User object matches the followers so that I can populate my ListView item with the right data from that Item object but this query returns zero results.
So either I am modeling everything wrong or I am misunderstanding how relationships work. Any help would be greatly appreciated.
If I remove the itemQuery and just return the followerQuery then it works - the problem is with the inner query when I try to combine the two (or it could be me complicating my life with the data structure in the database so I'm also open to suggestions on restructuring).
Thanks for your help in advance!
Upvotes: 1
Views: 1294
Reputation: 2972
The answer was to not use whereMatchesQuery()
but whereMatchesKeyInQuery()
instead.
here is an example using whereMatchesKeyInQuery()
(that is not necessarily specific to this question but demonstrating the use of whereMatchesKeyInQuery
by request in the comments):
ParseQuery followerQuery = ParseQuery.getQuery("Followers");
followerQuery.whereEqualTo("following", ParseUser.getCurrentUser());
userQuery.whereMatchesKeyInQuery("objectId", "followerId", followerQuery);
So the parameters would be:
query1.whereMatchesKeyInQuery("key_value_in_query1", "key_value_to_match_in_query2", query2);
Upvotes: 1