'When exactly is a read on firebase firestore performed

I'm reading a lot about "how to minify firebase read actions" and now trying to implement a function that caches the data and only updates modified one.

(Project is made with Dart and Flutter) For this purpose, lets assume i have a subcollection containing 10 documents, each holding at least the field "lastModified".

here's my example:

GetOptions _CACHE = GetOptions(source: Source.cache);  
GetOptions _SERVER = GetOptions(source: Source.server);
DateTime lastModified;

FirebaseFirestore firestore = FirebaseFirestore.instance;
    CollectionReference collectionReference = firestore.collection("collection").doc("document").collection("collection");
    Query lastModifiedQuery = collectionReference.orderBy("lastModified", descending: true);

    QuerySnapshot snapshot = await collectionReference.get(_CACHE);

    if (snapshot.docs.isEmpty)
    {
      QuerySnapshot querySnapshot = await lastModifiedQuery.limit(1).get(_SERVER);

      if (querySnapshot.size > 0)
      {
        lastModified = querySnapshot.docs.first.get("lastModified");
      } else {
        lastModified = DateTime.now();
      }
    } else {

      Query queryRefresh = collectionReference.orderBy("lastModified", descending: true).where("lastModified", isGreaterThan: lastModified);
   }

Now i am unsure about the different calls. I know that i am firing a read operation on every document if i call ".get()". But does just creating a query also runs a database operation and, in my case, updates the cache? Or do i have to run a ".get()" on the query and i am done?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source