0

I want to be able to stop snapshot listener when I don't need to listen for changes so later I can get all the updates at once. Is it possible to temporarily halt the snapshot listener? I think you have to remove it explicitly and reinitialize everything according to the doc. So I can explicitly call remove and reinitialize the snapshot listener to get changes, but is there a price penalty for doing this? I know reading cached values within 30 minutes doesn't cost anything, so does this mean it wouldn't cost anything to attach and detach the snapshot listener?

If a document has no changes, and I attach the snapshot listener over and over say 50 times in 30 minutes, would that cost me anything?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
coolcool1994
  • 3,704
  • 4
  • 39
  • 43

1 Answers1

2

Firebase Does it cost to attach and detach SnapshotListener many times when there are no changes within 30 minutes?

No, the listener will not fire if there are no changes in your database. However, if you detach the listener and you attach it again, there is a cost of one document read, even if the query returns no results. According to Frank van Puffelen's comment, here's the reason why it works that way:

The server needs to perform a query on your behalf, so if there are no individual documents being read on the server, it charges you for the query (actually reading the index) to prevent constant detach/reattaches (which use up server resources).

And it really makes sense.

I think you have to remove it explicitly and reinitialize everything according to the doc.

Yes, that's correct. When the listener is no more needed, simply detach it.

So I can explicitly call remove and reinitialize the snapshot listener to get changes, but is there a price penalty for doing this?

You'll always be billed for the number of documents that you read. For instance, if your query returns two documents, you be billed with two document reads. If you get no results, you only be billed with a single read operation.

I know reading cached values within 30 minutes doesn't cost anything, so does this mean it wouldn't cost anything to attach and detach the snapshot listener?

If a document has no changes, and I attach a snapshot listener over and over say 50 times in 30 minutes, would that cost me anything?

If you detach the listener and attach a new one for 50 times, and if there are no changes in the database, it will cost you 50 document reads.

Community
  • 1
  • 1
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I think your first answer may be slightly wrong. OP is talking about detaching the listener and attaching a new one. In that case, the client checks with the server for updates, in which case there's a minimum charge of 1 document read even if there are no changes. – Frank van Puffelen Jan 15 '20 at 15:49
  • @FrankvanPuffelen That's right. I just updated my answer with the correct approach. Thank you puf. – Alex Mamo Jan 15 '20 at 16:02
  • Still not completely correct. The minimum charge of one read happens even if you re-attach the listener within 30 minutes. I like to think of it as: the server needs to perform a query on your behalf, so if there are no individual documents being read on the server, it charges you for the query (actually reading the index) to prevent constant detach/reattaches (which use up server resources). – Frank van Puffelen Jan 15 '20 at 16:47
  • @FrankvanPuffelen Updated again. I hope is more clear now. Thank you. – Alex Mamo Jan 15 '20 at 17:21
  • The answer here seems different from what Doug said [here](https://stackoverflow.com/a/62472832/12208004). I'm kinda confused... – user12208004 Aug 11 '20 at 13:49
  • @user12208004 I think the misunderstanding comes from the fact that here I'm talking about realtime updates. I think this [article](https://medium.com/firebase-tips-tricks/how-to-drastically-reduce-the-number-of-reads-when-no-documents-are-changed-in-firestore-8760e2f25e9e) will help you understand the billing mechanism. – Alex Mamo Aug 11 '20 at 14:03
  • @AlexMamo Thanks for your reply I read your article. You answered "If you detach the listener and attach a new one for 50 times, and if there are no changes in the database, it will cost you 50 document reads.", this means that if I attach the listener to the collection which has 50 documents, and detach and attach again 10 times within 30 min, the cost will be 500 documents reads? – user12208004 Aug 11 '20 at 14:20
  • @user12208004 In the worst-case scenario, if the listener is removed every 31 minutes, you'll have to pay the exact number of reads that your query returns. – Alex Mamo Aug 11 '20 at 14:39
  • Thank you @AlexMamo! You answered on the top "Firebase Does it cost to attach and detach... No, the listener will not fire if there are no changes in your database.", but if @Doug said was true [here](https://stackoverflow.com/a/48481725/12208004), manually attaching and detaching the listener, like opening and closing the app, the cost will be doubled each time even without any updated document and within 30 min. Am I wrong? – user12208004 Aug 12 '20 at 02:15