Cloud Firestore: retrieving cached data via direct

2020-03-02 02:10发布

Retrieving data from the server may take some seconds. Is there any way to retrieve cached data in the meantime, using a direct get?

The onComplete seems to be called only when the data is retrieved from the server:

db.collection("cities").whereEqualTo("state", "CA").get()
        .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                ...
                }
            }
        });

Is there any callback for the cached data?

3条回答
Explosion°爆炸
2楼-- · 2020-03-02 02:52

I just ran a few tests in an Android app to see how this works.

The code you need is the same, no matter if you're getting data from the cache or from the network:

    db.collection("translations").document("rPpciqsXjAzjpComjd5j").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            DocumentSnapshot snapshot = task.getResult();
            System.out.println("isFromCache: "+snapshot.getMetadata().isFromCache());
        }
    });

When I'm online this prints:

isFromCache: false

When I go offline, it prints:

isFromCache: true

There is no way to force retrieval from the cache while you're connected to the server.

If instead I use a listener:

    db.collection("translations").document("rPpciqsXjAzjpComjd5j").addSnapshotListener(new DocumentListenOptions().includeMetadataChanges(), new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) {
                System.out.println("listen.isFromCache: "+snapshot.getMetadata().isFromCache());
            }
        }
    );

I get two prints when I'm online:

isFromCache: true

isFromCache: false

查看更多
成全新的幸福
3楼-- · 2020-03-02 02:52

Now it is possible to load data only from cached version. From docs

You can specify the source option in a get() call to change the default behavior.....you can fetch from only the offline cache.

If it fails, then you can again try for the online version.

Example:

DocumentReference docRef = db.collection("cities").document("SF");

// Source can be CACHE, SERVER, or DEFAULT.
Source source = Source.CACHE;

// Get the document, forcing the SDK to use the offline cache
docRef.get(source).addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            // Document found in the offline cache
            DocumentSnapshot document = task.getResult();
            Log.d(TAG, "Cached document data: " + document.getData());
        } else {
            Log.d(TAG, "Cached get failed: ", task.getException());
            //try again with online version
        }
    }
});
查看更多
何必那么认真
4楼-- · 2020-03-02 02:57

You can disable network access and run the query to access data from cache . For firestore : https://firebase.google.com/docs/firestore/manage-data/enable-offline#disable_and_enable_network_access For firebase database call db.goOffline() and db.goOnline()

查看更多
登录 后发表回答