Cloud Firestore onSnapshot() 能否仅在更改时触发,而无法获取初始状态? [英] Can Cloud Firestore onSnapshot() only trigger on changes, and not get the initial state?

查看:25
本文介绍了Cloud Firestore onSnapshot() 能否仅在更改时触发,而无法获取初始状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档 说:

您可以使用 onSnapshot() 方法收听文档.使用您提供的回调的初始调用会立即使用单个文档的当前内容创建文档快照.然后,每次内容改变时,另一个调用更新文档快照.

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.

我只想在数据更改时触发我的侦听器.我不希望它在应用加载时触发,以获取数据的初始状态.有什么建议吗?

I just want my listener to fire when the data changes. I don't want it to fire when the app loads, to get the initial state of the data. Any suggestions?

推荐答案

Firestore 侦听器不是这样工作的.您将始终收到与获取或查询相关的文档,然后只要侦听器保持添加状态就会更新.没有仅接收增量的模式.

Firestore listeners don't work that way. You will always be delivered the document(s) relevant to the fetch or query, then updates after that for as long as the listener remains added. There is no mode to receive deltas only.

如果您只想接收某些数据,您可能想弄清楚如何查询它,例如,通过添加时间戳字段并让客户端仅查询自之前某个时间以来已更改的文档.

If you want to receive only certain data, you might want to figure out how to query for it, for example, by adding a timestamp field and having the client only query for documents that have changed since some prior time.

这篇关于Cloud Firestore onSnapshot() 能否仅在更改时触发,而无法获取初始状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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