【问题标题】:Increasing Memory Usage with Firebase Auth & React Native使用 Firebase Auth 和 React Native 增加内存使用量
【发布时间】:2018-09-21 20:12:23
【问题描述】:

我正在尝试将 Firebase 身份验证功能添加到我的 React Native 应用程序中。由于 FirebaseAuth 是异步加载的,因此必须使用 onAuthStateChanged 来监听更改,如下所示:

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


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

我的实现是否存在导致内存使用量增加的问题,或者 firebase-js-sdk 是否存在问题?此外,是否有提供相同功能的代码替代方案?

(我使用的是 Expo 和 Firebase JS SDK 的 5.5.0 版本)

【问题讨论】:

    标签: javascript reactjs firebase react-native firebase-authentication


    【解决方案1】:

    我发现的唯一一件事是您可能应该在组件卸载后停止收听。您调用的方法返回一个函数,您可以调用它来取消订阅事件。比如:

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

    【讨论】:

    • 我创建了一个单独的组件,它使用了取消订阅方法,但我的内存使用量仍在增加,我还迁移了我的项目以使用本机模块 react-native-firebase 并且错误没有消失。
    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多