简体   繁体   English

使用Firebase Auth和React Native增加内存使用量

[英]Increasing Memory Usage with Firebase Auth & React Native

I am trying to add Firebase Authentication functionality to my React Native app. 我正在尝试将Firebase身份验证功能添加到我的React Native应用程序中。 As FirebaseAuth is loaded asynchronously, the onAuthStateChanged must be used to listen for changes, as implemented here: 由于FirebaseAuth是异步加载的,因此必须使用onAuthStateChanged侦听更改,如下所示:

  async componentDidMount() {
      if (!firebase.apps.length) { firebase.initializeApp(ApiKeys.FirebaseConfig); }


      firebase.auth().onAuthStateChanged((user) => {
          this.setState({
              isAuthenticationReady: true, isAuthenticated: !!user
          });
      });
}

Is there a problem with my implementation which causes increasing memory usage or is is a problem with the firebase-js-sdk? 我的实现是否存在问题,导致内存使用量增加,或者firebase-js-sdk存在问题? Additionally, are there any alternatives to the code which provide the same functionality? 另外,代码是否有提供相同功能的替代方法?

(I am using Expo and the version 5.5.0 of the Firebase JS SDK) (我正在使用Expo和Firebase JS SDK的5.5.0版本)

The only thing I spot is that you should probably stop listening after the component unmounts. 我发现的唯一一件事是,在卸载组件后,您可能应该停止监听。 The method you're calling returns a function you can call to unsubscribe to the events. 您正在调用的方法返回一个函数,您可以调用该函数来取消订阅事件。 Something like: 就像是:

componentDidMount() {
    this.onAuthStateChangedUnsubscribe = 
        firebase.auth().onAuthStateChanged((user) => {
            this.setState({
                isAuthenticationReady: true, isAuthenticated: !!user
            });
        });
}

componentWillUnmount() {
    if (this.onAuthStateChangedUnsubscribe) this.onAuthStateChangedUnsubscribe();
}

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

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