Parse.com Relational Query - Narrowing Down Results Issue

131 views Asked by At

I have a ParseQueryAdapter that will correctly display all posts if I allow it, but I'm trying to find a way to only display posts from friends. The relative bit of code is as follows:

public CustomAdapter(Context context) {
    // Use the QueryFactory to construct a PQA that will only show
    // Todos marked as high-pri
    super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {

            ParseUser user = ParseUser.getCurrentUser();

            ParseRelation<ParseUser> relation = user.getRelation("friends");
            ParseQuery relationQuery = relation.getQuery();
            relationQuery.whereExists("objectID");

            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Post");
            query.whereEqualTo("CreatedBy", relationQuery.whereExists("ObjectID"));
            return query;
        }
    });
}

The data structures are as follows. A user has a friends relation. So I want to get that relation from them and look for objectID's which match the CreatedBy user of a post. One of the problems that I'm seeing is that the CreatedBy field is a pointer to a user, where the objectID in the Friend relation is a string. I've tried a lot of different things here, but I'm having trouble getting it to work.

So I'm not sure if I should be converting CreatedBy to a string, but I'm not sure how to do so where it would work for the entire query. Or perhaps I should be looking at each friend in the friends relation as a user, rather than looking at the objectID field.

1

There are 1 answers

1
Timothy Walters On BEST ANSWER

What you want is the whereMatchesQuery() method, e.g.:

public CustomAdapter(Context context) {
    super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {

            ParseUser user = ParseUser.getCurrentUser();

            ParseRelation<ParseUser> relation = user.getRelation("friends");
            ParseQuery friendsQuery = relation.getQuery();

            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Post");
            query.whereMatchesQuery("CreatedBy", friendsQuery);
            return query;
        }
    });
}

You're basically saying "find me rows where the pointer in 'CreatedBy' matches one of the rows returned by this query".

The method is documented under Relational Queries.