2
我是Google Cloud FireStore中的新成员。如何删除DocumentSnapshot事件的侦听器(Google Cloud FireStore)
Document对象具有一个函数调用onSnapshot来附加DocumentSnapshot事件的侦听器。
是否有删除该侦听器(如offSnapshot)的函数?如果不是我怎么能实现它?
我是Google Cloud FireStore中的新成员。如何删除DocumentSnapshot事件的侦听器(Google Cloud FireStore)
Document对象具有一个函数调用onSnapshot来附加DocumentSnapshot事件的侦听器。
是否有删除该侦听器(如offSnapshot)的函数?如果不是我怎么能实现它?
如果使用web和node.js SDK,调用onSnapshot
会返回一个函数,您需要将该函数保存在变量中,并在要删除侦听器时调用。
var unsubscribe = db.collection("cities").onSnapshot(function (querySnaphot) {
// do something with the data.
});
// Stop listening to changes
unsubscribe();
其他SDK提供类似的功能。
请参阅https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener以供参考。
它工作正常。谢谢@Scarygami –