简体   繁体   English

更改一项后收听集合时,Firestore 读取了多少?

[英]How many Firestore reads when listening to a collection after changing one item?

Suppose I have a Firestore collection containing 10 items, and my app listens to a snapshot of that collection to get realtime updates, showing the results in a list view.假设我有一个包含 10 个项目的 Firestore 集合,我的应用监听该集合的快照以获取实时更新,并在列表视图中显示结果。

If I update one document inside that collection, a new snapshot will be emitted, so I can rebuild the list view.如果我更新该集合中的一个文档,将发出一个新的快照,这样我就可以重建列表视图。

But how many reads will take place?但是会发生多少次读取?

Do I get 10 additional reads since the entire list view gets rebuilt (as I have a listener to the entire collection)?自从重建整个列表视图(因为我有整个集合的侦听器)后,我是否获得了 10 次额外的读取?

Or is the Firebase SDK smart enough to figure out that only one document has changed and compute the new snapshot by taking the diff from the previous (cached?) one, resulting in one read only?或者 Firebase SDK 是否足够聪明,可以弄清楚只有一个文档发生了变化,并通过与前一个(缓存的?)文档的差异来计算新快照,从而导致一个只读?

This is what the documentation says in this regard:这就是文档在这方面所说的:

Listening to query results监听查询结果

Cloud Firestore allows you to listen to the results of a query and get realtime updates when the query results change. Cloud Firestore 允许您监听查询结果并在查询结果发生变化时获得实时更新。

When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated.当您收听查询结果时,每次添加或更新结果集中的文档时,您都需要为一次读取付费。 You are also charged for a read when a document is removed from the result set because the document has changed.由于文档已更改而从结果集中删除文档时,您也需要为阅读付费。 (In contrast, when a document is deleted, you are not charged for a read.) (相比之下,删除文档后,您无需为阅读付费。)

My interpretation is that if I do this:我的解释是,如果我这样做:

collectionRef.snapshots().listen((event) { ... });

then I should get 1 read per change.那么每次更改我应该读 1 次。

But what if I use collectionRef.snapshots() as a stream to rebuild the UI (without an explicit listener)?但是,如果我使用collectionRef.snapshots()作为 stream 来重建 UI(没有显式侦听器)怎么办? Will this result in 1 read or N reads?这会导致 1 次读取还是 N 次读取?

This was originally marked as duplicate of this .这最初被标记为this的副本。 But the question is different as I'm asking about listening to a parent collection when a document changes (not listening to the document itself).但是问题是不同的,因为我问的是在文档更改时收听父集合(而不是收听文档本身)。

You will only have one read for your scenario, but after 30 minutes you will be charged for 10 reads and then after another 30 minutes, you will be charged for another 10 reads and so on...您的场景只有一次阅读,但 30 分钟后,您将按 10 次阅读收费,然后再过 30 分钟,您将再按 10 次阅读收费,依此类推...

I don't know why Cloud Firestore charges to reread the entire collection after 30 minutes.我不知道为什么 Cloud Firestore 会在 30 分钟后重新读取整个集合收费。 The data is already in the local cache.数据已经在本地缓存中。 It should only need to read the changes since the last listener was active.它应该只需要读取自上一个侦听器处于活动状态以来的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM