【问题标题】:How to remove listener for DocumentSnapshot events (Google Cloud FireStore)如何删除 DocumentSnapshot 事件的侦听器(Google Cloud FireStore)
【发布时间】:2018-03-20 10:32:13
【问题描述】:

我是 Google Cloud FireStore 的新手。

Document 对象有一个函数调用onSnapshot 来附加一个DocumentSnapshot 事件的侦听器。

是否有移除该监听器的功能(如 offSnapshot)?如果没有,我该如何实现?

【问题讨论】:

    标签: firebase google-cloud-platform google-cloud-firestore


    【解决方案1】:

    在 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。

    【讨论】:

    • 在 ts angular 中做什么?
    • 我们需要在 Angular 中这样做吗?
    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2022-08-03
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    相关资源
    最近更新 更多