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

爷,独闯天下 提交于 2020-04-30 11:43:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/59754571/firebase-does-it-cost-to-attach-and-detach-snapshotlistener-many-times-when-ther

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!