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.