简体   繁体   English

Firebase:如何停止收听快照

[英]Firebase: How to stop listening to snapshot

I have a javascript application that uses the onSnapshot listener to listen to changes in my firebase collection. 我有一个JavaScript应用程序,该应用程序使用onSnapshot侦听器侦听Firebase集合中的更改。 The docs tell me to unsubscribe when I am no longer in need of the listener. 当我不再需要侦听器时,文档告诉我取消订阅。 In React I would do this in componentWillUnmount() but if I've set up the listener in a vanilla class (not React), how would I unsubscribe in this situation? 在React中,我将在componentWillUnmount()执行此操作,但是如果我在普通类(不是React)中设置了侦听器,在这种情况下我将如何退订?

How I'm implementing it... 我如何实现它...

class MyComponent extends React.Component {
  constructor() {
    super();
    this.presenter = new MyPresenter();
  }
  ...rest of component

class MyPresenter {
    constructor() {
      const listener = db.collection("cities")
        .onSnapshot(function () {});
    }
}

Seems to me that you'd just want to make a method on MyPresenter called "stop()" or something similar, which does the unsubscribe. 在我看来,您只想在MyPresenter上创建一个称为“ stop()”或类似方法的方法, MyPresenter取消订阅。 Then, in your component, call presenter.stop() during its componentWillUnmount() . 然后,在您的组件中,在其componentWillUnmount()期间调用presenter.stop() componentWillUnmount()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM