Removing Firebase authState upon viewDidDisappear

假如想象 提交于 2019-12-10 15:56:38

问题


I'm using Firebase's new framework and I'm attempting to monitor the login state of the user on both the Login and Signup VC's separately. The problem is if the login state changes on the SignUp view the Auth State on the Login view gets called as well. My question is, how do I remove the auth state? I found the syntax on the Firebase website but am a little confused on what to pass in considering my code for the auth state:

FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
            if let theUser = user {
                // User is signed in.
                print("LOGGED IN!!!!  :::: \(theUser)")
                self.dismissViewControllerAnimated(true, completion: nil)
            } else {
                // No user is signed in.
                print("Need to login first.")
            }
        }

Code to use to remove the auth, but unsure what to pass in.

FIRAuth.auth()?.removeAuthStateDidChangeListener(FIRAuthStateDidChangeListenerHandle) 

Says I pass in a FIRAuthStateDidChangeListenerHandle, but how do I obtain this, or do I rewrite my authState code differently?


回答1:


Just store the auth in a variable

self.authListener = FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
            if let theUser = user {
                // User is signed in.
                print("LOGGED IN!!!!  :::: \(theUser)")
                self.dismissViewControllerAnimated(true, completion: nil)
            } else {
                // No user is signed in.
                print("Need to login first.")
            }
        }

and remove it later

FIRAuth.auth()?.removeAuthStateDidChangeListener(self.authListener) 


来源:https://stackoverflow.com/questions/37642091/removing-firebase-authstate-upon-viewdiddisappear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!