Search code examples
javaandroidparse-platform

Parse Local Datastore Query


I'm trying to enable a cached result in my Android application which is using Parse Server by Back4App as a back-end.

I have local datastore enabled but I cannot seem to query from network / cached results.

The following code is from my query

        query.fromLocalDatastore().findInBackground()
                .continueWithTask((task) -> {
                    if (task.isFaulted() && task.getError() instanceof ParseException && ((ParseException) task.getError()).getCode() == ParseException.CACHE_MISS) {
                        return query.fromNetwork().findInBackground();
                    }
                    Log.d("Cache", "" + task.getResult().size());
                    return task;
                }, Task.UI_THREAD_EXECUTOR)
                .continueWithTask((task) -> {
                    // Update UI with results ...
                    Log.d("Network", "" + task.getResult().size());
                    ParseObject.pinAllInBackground(task.getResult());
                    return task;
                }, Task.UI_THREAD_EXECUTOR);

The logcat shows both Cache and Network with size of 0.

I have tried the following:

        query.fromLocalDatastore().findInBackground()
                .continueWithTask((task) -> {
                    // Update UI with results from Local Datastore ...
                    ParseException error = (ParseException) task.getError();
                    if (error == null) {
                        Log.d("Cache", "" + task.getResult().size());
                    }
                    // Now query the network:
                    return query.fromNetwork().findInBackground();
                }, Task.UI_THREAD_EXECUTOR)
                .continueWithTask((task) -> {
                    // Update UI with results from Network ...
                    ParseException error = (ParseException) task.getError();
                    if (error == null) {
                        Log.d("Network", "" + task.getResult().size());
                    }
                    return task;
                }, Task.UI_THREAD_EXECUTOR);

which executes cache followed by network and it is working. But I want Cache else Network if error is not null.


Solution

  • It seems that your objects have never been pinned so your local query always succeed returning 0 objects and it continues not pinning anything again. Try something like:

                query.fromLocalDatastore().findInBackground()
                    .continueWithTask((task) -> {
                        ParseException error = (ParseException) task.getError();
                        if (error != null || task.getResult().size() == 0) {
                            return query.fromNetwork().findInBackground();
                        }
                        Log.d("Cache", "" + task.getResult().size());
                        return task;
                    }, Task.UI_THREAD_EXECUTOR)
                    .continueWithTask((task) -> {
                        // Update UI with results ...
                        Log.d("Network", "" + task.getResult().size());
                        ParseException error = (ParseException) task.getError();
                        if (error == null) {
                            ParseObject.pinAllInBackground(task.getResult());
                        }
                        return task;
                    }, Task.UI_THREAD_EXECUTOR);