1

當我打電話試試時,有沒有人知道這個原因?Firebase在註銷時會出現異常

FIRAuth.auth()?.signOut()

這種情況 error here

ANSWER 當我們調用signout,用戶UID變爲零,而觀察者仍然需要爲uid刪除觀察者。因此,要解決它,我用addAuthStateDidChangeListener

FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth: FIRAuth,user: FIRUser?) in 
       if user != nil { 
        let controller = self.storyboard?.instantiateViewControllerWithIdentifier("ProfileViewController") as! ProfileViewController 
        controller.userID = (user?.uid)! 
        self.presentViewController(controller, animated: false, completion: nil) 
       } 
       else { 
        let controller = self.storyboard?.instantiateViewControllerWithIdentifier("LoginRegisterViewController") as! LoginRegisterViewController 
        self.presentViewController(controller, animated: false, completion: nil) 
       } 
      }) 
+1

是'User'零代碼?將'print(user == nil)'放在代碼中斷的行之前。查看是否將「true」打印到控制檯。 –

+0

我確保用戶不是n,而且我已經嘗試使用observeSingleEventOfType 所以一旦我登錄,我將從數據庫中提取我的用戶信息,信息已顯示在我的用戶界面上。所以之後當我想註銷時,會發生運行時錯誤。 –

+0

很棒,你找到了解決你自己的問題。你能把它作爲答案發布嗎?自我回答在StackOverflow中完全可以接受,並且如果它們是真正的答案,則可以更清楚地進行跟蹤。 –

回答

1

問題 當我去這個觀點,我會打電話給觀察者功能,這是我傳遞(用戶?.uid)!但是當我們註銷時,(user?.uid)!當觀察者仍然需要調用(user?.uid)時,它將爲零!

解決方案 因此,要解決這個問題,我創建了一個叫做用戶ID在類變量和傳遞(用戶?.uid)!以用戶ID當我移動到該視圖

這裏是移動到該視圖

FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth: FIRAuth,user: FIRUser?) in 
      if user != nil { 
       let controller = self.storyboard?.instantiateViewControllerWithIdentifier("ProfileViewController") as! ProfileViewController 
       controller.userID = (user?.uid)! 
       self.presentViewController(controller, animated: false, completion: nil) 
      } 
      else { 
       let controller = self.storyboard?.instantiateViewControllerWithIdentifier("LoginRegisterViewController") as! LoginRegisterViewController 
       self.presentViewController(controller, animated: false, completion: nil) 
      } 
     })