如何删除 DocumentSnapshot 事件的侦听器 (Google Cloud FireStore) [英] How to remove listener for DocumentSnapshot events (Google Cloud FireStore)
问题描述
我是 Google Cloud FireStore 的新手.
I'm new in Google Cloud FireStore.
Document 对象有一个函数调用 onSnapshot 为 DocumentSnapshot 事件附加一个侦听器.
The Document object has a function call onSnapshot to attaches a listener for DocumentSnapshot events.
有没有删除那个监听器的函数(比如offSnapshot)?如果不是,我该如何实施?
Is there a function to remove that listener (like offSnapshot)? If not how can I implement it?
推荐答案
在 web 和 node.js SDK 的情况下,调用 onSnapshot
返回一个函数,你需要保存在一个变量中并调用当你想移除监听器时.
In case of the web and node.js SDK, calling onSnapshot
returns a function that you need to save in a variable and call when you want to remove the listener.
var unsubscribe = db.collection("cities").onSnapshot(function (querySnaphot) {
// do something with the data.
});
// Stop listening to changes
unsubscribe();
其他 SDK 提供类似的功能.
The other SDKs offer similar functionality.
参见 https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener 供参考.
这篇关于如何删除 DocumentSnapshot 事件的侦听器 (Google Cloud FireStore)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!