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

查看:52
本文介绍了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天全站免登陆