【问题标题】:Firebase onAuthStateChanged unsubscribe is not a function errorFirebase onAuthStateChanged unsubscribe 不是函数错误
【发布时间】:2018-06-22 04:16:54
【问题描述】:

在第一次安装时,应用程序运行没有任何问题。

  1. 在启动画面检查登录状态。
  2. 用户未登录,导航到登录屏幕。

unsubscribe() is not a function 错误显示并在应用程序最小化然后重新打开时卡在初始屏幕。这只发生在最小化 > 从全新安装和首次运行中恢复。

这意味着如果我从后台杀死应用程序并打开然后最小化然后恢复,它工作得很好。

这是我在 firebase 文档中使用的 checkLoginState 函数。我不确定这与react-nativefirebasereact-native-firebasereact-navigation 有关。知道如何调试和修复这个问题吗?

Handle the sign-in flow manually

function checkLoginState(event) {
  if (event.authResponse) {
    // User is signed-in Facebook.
    var unsubscribe = firebase.auth().onAuthStateChanged(function(firebaseUser) {
      unsubscribe();
      // Check if we are already signed-in Firebase with the correct user.
      if (!isUserEqual(event.authResponse, firebaseUser)) {
        // Sign in user.
      } else {
        // User is already signed-in Firebase with the correct user.
      }
   });
  } else {
    // User is signed-out of Facebook.
    firebase.auth().signOut();
  }
}

更新

在全新安装 > 首次运行 > 最小化 > 恢复`,应用程序看起来像堆栈或什么。如果我使用 react-native reload 重新加载应用程序,我会在日志中得到这个。 componentDidmount 和 unmount 运行多次。每个最小化>恢复堆栈1。如果我杀死应用程序并重新打开,这个问题就消失了。请注意,这在物理设备和虚拟设备中都会发生。

更新 1

这主要与本机反应有关。 https://github.com/facebook/react-native/issues/12562

【问题讨论】:

  • 我想它只发生在 Android 上,对吧?你会测试在你的 AndroidManifest.xml 上添加它,看看它是否修复了吗? `
  • 是的 android 但不确定 ios。我尝试了您的解决方案,但仍然出现同样的错误。
  • 尝试删除var unsubscribe =并删除第5行。
  • @MattAft 我无法删除取消订阅,因为如果用户单击注销按钮,onAuthStateChanged 触发然后 facebook,firebase 用户将在 isUserEqual() 进行比较并立即登录用户。我用 firebase 文档链接更新了问题。
  • 如果您设置了“SingleTask”,您的应用程序在进入后台时不应重新启动(不应进入 Splash)并通过按应用程序图标返回。我想确保您的应用能够做到这一点。

标签: firebase react-native firebase-authentication react-navigation react-native-firebase


【解决方案1】:

我也遇到了这个问题,对此我很头疼……根据将侦听器作为函数调用的文档,必须取消订阅侦听器,但事实并非如此。所以这就是我所做的,希望对你也有帮助..

class Home extends Component {


constructor(props) {
    super(props);
    this.Homelistener = null;
    this.state = {
          // ...
    };


this.Homelistener = firebase.auth().onAuthStateChanged(user => {
  if (user) {

   //do whatever you want here
   // you can unsubscribe from the listener here or do it in 
   //componentwillunmount()

    this.Homelistener = null; // --> just set it to null

  } else {

    //do something

    SplashScreen.hide();

  }
});

【讨论】:

    猜你喜欢
    • 2022-07-23
    • 2019-10-15
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2018-08-17
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多