Search code examples
androidparse-platformparse-serverparse-cloud-code

How to specify parse query to get parse objects only if they have been updated? [Android]


We are using parse server and mongodb. Users request ParseObjects and store them locally on their device with realm. Later on users have to check for updates from that objects. ParseObject offers the updatedAt attribute, so we would request for objects where the updatedAt has been changed. For a single object the query would look something like:

ParseObject object = new ParseObject();
ParseQuery<ParseObject> query = new ParseQuery<>(ParseObject.getQuery());
query.whereEqualTo("objectId", "9jK7lGt7U").whereGreaterThan("updatedAt", object.getUpdatedAt());
ParseObject queriedObject = query.findInBackground();

We have modified the model of the query for this post. ParseObject is the pseudo of our model.

As said before, the query has to be modified to work with many objects. The objectIds and updatedAts are known. How does the query must look like?


Solution

  • You can make a query fromLocalDatastore,

    ParseObject queriedObject = query.findInBackground();
    query.fromLocalDatastore();
    

    And after the query on each object, you can call on each object fetchIfNeeded

    On each object that you recieve you can do that:

    object.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
      public void done(ParseObject object, ParseException e) {
        // all fields of the object will now be available here.
      }
    });
    

    With your code you can write this:

    ParseQuery<ParseObject> query = new ParseQuery<>(ParseObject.getQuery());
    query.whereEqualTo("objectId", "9jK7lGt7U").whereGreaterThan("updatedAt", object.getUpdatedAt());
    query.fromLocalDatastore();
    ParseObject queriedObject = query.findInBackground();
    
    
    for( int i=0; i < queriedObject.length-1; i++)
    {
        queriedObject[i].fetchIfNeededInBackground();
    }
    

    I hope my answer solve your problem