How to read data only from cache in Firestore?

跟風遠走 提交于 2020-06-01 05:50:30

问题


I read some answers here on stackoverflow, were is said that every time we get documents from Firestore, the SDK is always trying to get the online version of the documents, even if no documents were changed. This is ending in having more reads billed, which in my opinion is not necessary, since nothing is changed.

What I want to achieve

Let's say a have a collection of 5 documents. When the user opens the app for the first, I want to pay 5 reads. However, when the user opens the app for the second time, I just want to pay a read operation only for documents that were changed. If nothing is changed, I don't want to pay any reads, I just want to read the data from cache. Is this possible?


回答1:


The key phrase in your question is:

If nothing is changed, I don't want to pay any reads,

To determine if something changes about the documents in your cache, the Firestore server will need to read those documents. And hence you will pay for those reads.

The only way to work around this, is to take control of filtering the changed documents yourself.

For example, if you include a lastModified field in each of your documents, you can use that to retrieve only the new/modified documents from the server, and then run your other read operations against the local cache by specifying source options.



来源:https://stackoverflow.com/questions/61933031/how-to-read-data-only-from-cache-in-firestore

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