如何仅从Firestore中的缓存读取数据? [英] How to read data only from cache in Firestore?

查看:52
本文介绍了如何仅从Firestore中的缓存读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了关于stackoverflow的一些答案,据说,每次我们从Firestore获取文档时,即使没有更改任何文档,SDK都始终尝试获取文档的在线版本.结局是需要支付更多的阅读费用,我认为这是没有必要的,因为什么都没有改变.

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.

我想实现的目标

假设一个有5个文档的集合.当用户第一次打开该应用程序时,我要支付5次读取.但是,当用户第二次打开该应用程序时,我只想仅对已更改的文档进行读取操作.如果什么都没有改变,我不想进行任何读取,我只想从缓存中读取数据.这可能吗?

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?

推荐答案

问题中的关键短语是:

如果什么都没改变,我不想支付任何费用,

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

要确定缓存中的文档是否有所更改,Firestore服务器将需要读取这些文档.因此,您将为这些读取操作付费.

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.

例如,如果您在每个文档中都包含一个 lastModified 字段,则可以使用该字段从服务器中仅检索新的/已修改的文档,然后针对该文档运行其他读取操作指定源选项.

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.

这篇关于如何仅从Firestore中的缓存读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆