Jorge Mendez
Jorge Mendez

Reputation: 674

Query efficiency with Parse using local store in Android

My question is very simple, what is the best approach to work with Parse using the local store at the time I want to query the saved objects.

Is it better to trigger several queries to the local store directly on the main thread and avoid nesting a lot of anonymous classes or using a background thread?

It's important thing to notice is that this method is going to be called very frequently and the pattern will be repeated in several places with different queries. I'm evaluating both efficiency and code quality in readability. These methods will be called synchronously so we can assume the data will be consistent at any time.

As the objects are being saved locally I would expect the queries to be very fast in response. Here's a rough sample of how the code would look like in both cases.

Option one:

public void processBatches() { 
    ParseQuery<Batch> batchQuery = Batch.getQuery();
    int batchCount = batchQuery.fromLocalDatastore().count();
    List<Batch> batches = batchQuery.fromLocalDatastore().find();
    for(Batch b : batches) {
       // do whatever I need to do
    }
}

Option two:

public void processBatches() { 
    ParseQuery<Batch> batchQuery = Batch.getQuery();
    int batchCount = batchQuery.fromLocalDatastore().countInBackground(new CountCallback() {
        @Override
        public void done(int i, ParseException e) {
            if (i > 0) {
                batchQuery.findInBackground(new FindCallback<Batch>() {
                    @Override
                    public void done(List<Batch> list, ParseException e) {
                        for (Batch batch : list) {
                            // do whatever I need to do
                        }
                    }
                });
            }
        }
    });
}

Upvotes: 4

Views: 253

Answers (2)

Lucas Crawford
Lucas Crawford

Reputation: 3118

Well since in option one you are blocking the UI thread, there could be a delay in the user's ability to interact with your application. This is not a very good option since even if it is for just a moment, users don't want to be waiting unless they know operations are happening. But, if you know that at any time there will be little to no delay, go ahead and do it.

Nevertheless, I argue that option two is going to be the best option. This is because, in general, all network operations should be performed in the background. Although in your case you are performing local datastore queries, suppose a user has gone to their application task manager and cleared the data (very rare this will happen) what happens now when you perform the find from local data store and processing of Batch objects? Well, the app crashes. Again, this is not a very good option for the usability for your application.

Choose the second option, and allow an AsyncThread to run the find() and count() query operations to the network if there is nothing found from local data store queries. Also, from the Parse documentation for find:

public Task<List<T>> findInBackground()
Retrieves a list of ParseObjects that satisfy this query from the source in a background thread.
This is preferable to using ParseQuery.find(), unless your code is already running in a background thread.

Returns:
A Task that will be resolved when the find has completed.

Parse's creators prefers that the users of their API use a background thread to perform operations.

Upvotes: 3

MobileMon
MobileMon

Reputation: 8651

It really depends.

Is the user triggering the update? If so then do it on the main thread because you don't want them waiting

If not, then is the data access a result of fetching data from the web (and hence you should already be on a background thread) so could probably just remain on the background thread

Also what happens in "// do whatever I need to do"? Is it an update to the UI or more background processing?

Upvotes: 1

Related Questions