0

I'm trying to understand when Firestore caches something. To do so, I run the following code: I would expect the first get to have isFromCache false and I would expect that the second get would come from cache.

FirebaseFirestore.instance.collection('questions').doc("xyz").get().then((DocumentSnapshot documentSnapshot) {
  print("1st: " + documentSnapshot.metadata.isFromCache.toString());

  FirebaseFirestore.instance.collection('questions').doc("xyz").get(GetOptions(source: Source.cache)).then((DocumentSnapshot documentSnapshot) {
    print("2nd: " + documentSnapshot.metadata.isFromCache.toString());
  });
});

However, when running this (dart, running in Chrome) I noticed the following logs so apparently, the document is not cached. Is this expected behavior? Am I missing something?

1st: false
zone.dart:1339 Uncaught [cloud_firestore/unavailable] Failed to get document from cache. (However, this document may exist on the server. Run again without setting 'source' in the GetOptions to attempt to retrieve the document from the server.)
    at Object.wrapException (http://localhost:41665/main.dart.js:2047:17)
    at Object.throwExpression (http://localhost:41665/main.dart.js:2061:15)
    at guard_closure0.call$1 (http://localhost:41665/main.dart.js:22924:16)
    at _RootZone.runUnary$2$2 (http://localhost:41665/main.dart.js:16321:18)
    at _RootZone.runUnary$2 (http://localhost:41665/main.dart.js:16325:19)
    at _FutureListener.handleError$1 (http://localhost:41665/main.dart.js:15447:19)
    at _Future__propagateToListeners_handleError.call$0 (http://localhost:41665/main.dart.js:15732:49)
    at Object._Future__propagateToListeners (http://localhost:41665/main.dart.js:5177:77)
    at _Future._completeError$2 (http://localhost:41665/main.dart.js:15577:9)
    at _Future__asyncCompleteError_closure.call$0 (http://localhost:41665/main.dart.js:15663:18)

EDIT: when adding a listener like below, I do get "1st: false, 2nd: true" as expected.

    FirebaseFirestore.instance.collection('questions').doc("xyx").snapshots().listen((event)
    {
      print("update received");
    });

    FirebaseFirestore.instance.collection('questions').doc("xyz").get().then((DocumentSnapshot documentSnapshot) {
      print("1st: " + documentSnapshot.metadata.isFromCache.toString());

      FirebaseFirestore.instance.collection('questions').doc("xyz").get(GetOptions(source: Source.cache)).then((DocumentSnapshot documentSnapshot) {
        print("2nd: " + documentSnapshot.metadata.isFromCache.toString());
      });
    });

Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52
  • Hi, Can you please check if this helps you?: https://stackoverflow.com/a/60321758/15774177 – Zeenath S N Oct 11 '21 at 14:47
  • @ZeenathSN Thank you for the interesting pointer. But the issue I have is not in the value of the isFromCache flag but the Document itself doesn't seem to be in the cache in the first place. – Marc Van Daele Oct 11 '21 at 17:19

1 Answers1

0

You are making a standard request as per your code and expecting it to be served from cache, this is a misunderstanding of how caching works.

Enabling offline access is required for caching and in the documentation it is stated that fromCache is only returned if the device is in offline mode and the document is in cache.

Zeenath S N
  • 1,100
  • 2
  • 8
  • I don't think this tells the complete story: when I add a listener to my document (see updated code in my question), I do get the data from cache. – Marc Van Daele Oct 15 '21 at 08:24
  • You can post your edit/code as an answer to help other community members and to avoid confusion. – Zeenath S N Oct 15 '21 at 10:13
  • Agreed, though I don't consider it to be an answer: the source.fromCache and metadata.isFromCache is still confusing and counter-intuitive to me – Marc Van Daele Oct 15 '21 at 10:51
  • [isFromCache](https://stackoverflow.com/a/59637177/15774177) tells us if the document is up to date or incomplete, and [fromCache](https://cloud.google.com/firestore/docs/manage-data/enable-offline#listen_to_offline_data) tells us if the document is from cache or the server – Zeenath S N Oct 19 '21 at 09:26