2

I didn't find any solutions to avoid reading data from the server when using get(). However, I might found a solution but it's not clear to me if it will work. I found that when using the real-time feature, the client will continuously update as the data changes. So per my understanding, if nothing is changed on the server, no reads charged, right?

However, I read that the listener should be removed, and I understood why, what I cannot understand is, if I close the app (listener is removed) and I open the app the second day, am I charged again for the data that was cached a day before?

I'm really confused because I also read that:

  • Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.

Removing the listener and going online, are not the same exact thing?

Lis Dya
  • 317
  • 3
  • 14

1 Answers1

3

I found that when using the real-time feature, the client will continuously update as the data changes. So per my understanding, if nothing is changed on the server, no reads charged, right?

Every query that reaches the server will incur reads for documents returned by the query. Whenever a document is returned from the server, it costs a read. If you have a listener on a set of query results where only one document changes while the listener is active, it costs one read, because only one document must come from the server, and the rest are already in memory. They stay in memory until the listener is removed.

if I close the app (listener is removed) and I open the app the second day, am I charged again for the data that was cached a day before?

Yes. Whenever the results come from the server, you will be billed for those reads. The cache is not used to satisfy query results when using the server as a source.

Removing the listener and going online, are not the same exact thing?

They are not the same thing. Removing a listener says that you're completely done with the results of the query. Going online temporarily and coming back online just resumes the existing query.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks Doug. You say *Whenever the results come from the server, you will be billed for those reads.* But my question was, if the results of a query already exists in the cache, as I already got them a day before, am I billed for that? – Lis Dya May 23 '20 at 17:54
  • If you're not querying the cache specifically, then documents don't come from the cache, unless the client is offline. Read about specifying the source of the query. https://firebase.google.com/docs/firestore/query-data/get-data#source_options – Doug Stevenson May 23 '20 at 18:03
  • I'm not using get(), I'm listening for real-time updates. In short, today I open the app, I get 30 docs. I close the app, the listener is removed. Second day I open the app, listener is attached, am I charged for those 30 docs again? Those 30 docs are the result of the same query and no change was made in meantime. – Lis Dya May 23 '20 at 18:09
  • It sounds like you're still asking the same question. You are charged for all the results regardless of cache. The bottom line is this: **unless you are offline or querying the cache explicitly, you are charged for all results that come from the server**. – Doug Stevenson May 23 '20 at 18:21