2

So I'm Implementing an app using Parse.com as back end, There is basically 3 class in Parse. The first one User where I have the User's Information, and Gallery where I have images "like Instagram, and Finally Follow Class where I have the relation following and followers.

Now I have an activity where I have to display all the images for the people I'm following only. I couldn't write a correct relational queries to get the correct result.

so this is the code that display all the images in the database not only for the people I'm following.

public class FollowingImages extends ParseQueryAdapter<ParseObject> {



public FollowingImages (Context context) {

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

            ParseQuery<ParseObject> query = ParseQuery.getQuery("Gallery");
            query.include("createdBy");
            return query;

        }
    });
}

// Customize the layout by overriding getItemView
@Override
public View getItemView(final ParseObject object, View v, ViewGroup parent) {

    if (v == null) {
        v = View.inflate(getContext(), R.layout.list_item, null);
    }

    super.getItemView(object, v, parent);

    // retrieving the Images from Parse.com

    return v;
}

I don't know where should I apply the relational query in the constructor or inside getItemView.

please any help would be highly appreciated.

Ran
  • 145
  • 2
  • 11

2 Answers2

1

You could use the matchesKeyInQuery constraint method.

You can use the matchesKeyInQuery method to get objects where a key matches the value of a key in a set of objects resulting from another query.

(after parse docs)

So in your case (I am writing it by hand) it may be something like:

ParseQuery<Follow> whoDoIFollow = ParseQuery.getQuery("Follow")
                                          .select("From")
                                          .whereEqualTo("To", <user>);

ParseQuery<Gallery> theirImages = ParseQuery.getQuery("Gallery")
                                          .whereMatchesKeyInQuery("createdBy", "From", whoDoIFollow);

If I am correct that should be an optimal way from the point of data transferred as everything happens on the server side and you only get the results(images).


Just replace

public FollowingImages (Context context) {

    super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
             ParseQuery<ParseObject> whoDoIFollow = ParseQuery.getQuery("Follow")
                                                         .select("From")
                                                         .whereEqualTo("To", <user>);

             ParseQuery<ParseObject> theirImages = ParseQuery.getQuery("Gallery")
                                                         .whereMatchesKeyInQuery("createdBy", "From", whoDoIFollow);

             return theirImages;
        }
    });
}
Dave S
  • 3,378
  • 1
  • 20
  • 34
MatBos
  • 2,380
  • 24
  • 34
  • You linked to JavaScript docs and constrained the query to values that don't exist for the User class but for the Follow class. – Dave S Aug 11 '15 at 21:05
  • 1
    Keep in mind that the default results limit is 100 objects which can result in strange looking results... – Wain Aug 11 '15 at 21:05
  • 2
    you should edit your answer to include the right docs for the tag and edit the code example to be correct and I'll remove the downvote. – Dave S Aug 11 '15 at 21:06
  • 1
    Thanks for spotting that. Corrected. – MatBos Aug 11 '15 at 21:10
1

You will want to apply constraints to your query.

ParseQuery<ParseObject> query = ParseQuery.getQuery("Gallery");

query.whereEqualTo("createdBy", "theUserId");
query.findInBackground(new GetCallback<ParseObject>() {
  public void done(ParseObject object, ParseException e) {
    if (object == null) {
      Log.d("score", "The find request failed.");
    } else {
      Log.d("score", "Retrieved the object.");
    }
  }
});

See the official guide for more details.

Dave S
  • 3,378
  • 1
  • 20
  • 34
  • it's not only one userId , I want to see the images from all the users I'm following – Ran Aug 11 '15 at 20:52
  • you can combine multiple conditions using the or() function: https://parse.com/docs/android/api/com/parse/ParseQuery.html#or%28java.util.List%29 – Dave S Aug 11 '15 at 20:55
  • You will want to first Query the list of followers to find the user IDs you need. Then you can create a List of Queries and combine them into a single Query using the or() method. I recommend you apply my code to get just a single person you are following then once that is working attempt to combine them in a single query using or(). – Dave S Aug 11 '15 at 21:02
  • in order to combin with or() , they have to be from one class . Like all the query from Follow class or gallery class. – Ran Aug 11 '15 at 21:07
  • My example shows you how to get the Gallery for a single userID, you can make many of these queries and combine them with or() before you findInBackground. You can additionally use the information MatBos provided to get the list of users you are Following. – Dave S Aug 11 '15 at 21:09
  • Thank you so much, you're the best !! – Ran Aug 12 '15 at 17:24
  • can you please check this question out http://stackoverflow.com/questions/32056085/how-to-set-and-get-relational-data-in-parse-com-android – Ran Aug 17 '15 at 17:12