1

After i execute my Firestore-Test-App made with Flutter i look at the Firestore analytics to see how many request my Test-App made. It shows me that i make a minimum of 20 up to 60 document reads with 1 start of the Test. The problem is, the test should result in a maximum of ~1-3 reads as i understand it.

I've read https://firebase.google.com/docs/firestore/pricing. It did help to understand the billing logic of firestore, but following that logic in the article i should be making a maximum of ~5 reads. This thread: Firestore - unexpected reads also suggests that maybe, the document reads come from the opened Firebase console, viewing the documents. So i closed it before the test and opened it 30 min after. This did not change the result. I also set breakpoints and the code did only execute once.

I opened a completly new Flutter Project to test it. This is the only part making read requests:

 CollectionReference dbUsers = dbInstance.collection("Users"); 
 var user = dbUsers
      .where("docId", isEqualTo: fireAppUser.user.uid)
      .limit(1)
      .snapshots();

  var _userSub = user.listen((value) {
    if (value.documents.isNotEmpty && value.documents.first.data != null)
      print(value.documents.first.data);
  });
_userSub.cancel()

Below my firestore rule, which is on default settings for now.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

I have exactly 5 documents in my Database.

I expect to have a maximum of ~5 document reads. Please help me to undestand why this Snippet causes an unexpected number of reads. What could cause it?

Edit: forgot to append the cancel() to the snippet

Marc Ma
  • 318
  • 2
  • 12

1 Answers1

0

That code can make a nearly unlimited number of reads, since it's adding a listener to the document. It will cost 1 read the first time you run it, and continue to incur reads as the listener remains added, as the document changes over time.

If the accounting doesn't make sense to you, contact Firebase support with your exact reproduction instructions.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441