Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore: can cache be read synchronously?

Tags:

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?

like image 677
Daniele B Avatar asked Nov 09 '17 09:11

Daniele B


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.

like image 57
Alex Mamo Avatar answered Sep 22 '22 14:09

Alex Mamo