如何删除DocumentSnapshot事件的侦听器(Google Cloud FireStore)

时间:2017-10-09 09:07:57

标签: firebase google-cloud-firestore

我是Google Cloud FireStore的新手。

Document对象具有函数调用onSnapshot,用于附加DocumentSnapshot事件的侦听器。

是否有删除该侦听器的功能(如offSnapshot)?如果不是,我该如何实施呢?

1 个答案:

答案 0 :(得分:29)

对于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以供参考。