Cloud Firestore: can cache be read synchronously?

Viewed 1170

Can documents be retrieved synchronously from the cache?

For Android, the documentation only gives an asynchronous example?

DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            ...
        }
    }
});

UPDATE:

It seems to me that the onComplete gets called only when the data is coming from the server. It doesn't seem to get ever called with cached data.

Can anyone confirm?

1 Answers

The official documentation only gives an asynchronous example because a synchronous one does not exist. All read operations that are done either from the cache or either online directly from Firebase servers, are asynchronous and you cannot change this behaviour.

What you are asking can be done on a regular JVM, with regular Java synchronization primitives but this won't work on Android.

Related