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

Min*_*yen 16 firebase google-cloud-platform google-cloud-firestore

我是Google Cloud FireStore的新手.

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

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

Sca*_*ami 39

对于web和node.js SDK,调用onSnapshot返回一个函数,您需要将其保存在变量中,并在要删除侦听器时调用.

var unsubscribe = db.collection("cities").onSnapshot(function (querySnaphot) {
  // do something with the data.
});


// Stop listening to changes
unsubscribe();
Run Code Online (Sandbox Code Playgroud)

其他SDK提供类似的功能.

有关参考,请参阅https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener.

  • 在 ts angular 中做什么? (3认同)
  • 我们需要以角度来做这件事吗? (2认同)