Swift - Firebase function signInWithEmail don't execute in the first call

て烟熏妆下的殇ゞ 提交于 2021-01-27 20:36:02

问题


@IBAction func loginEmailButton(sender: AnyObject) {
        FIRAuth.auth()?.signInWithEmail(email.text!, password: password.text!, completion: { (user, error) in
            if error != nil {
                if let errCode = FIRAuthErrorCode(rawValue: error!.code) {
                    switch errCode {
                    case .ErrorCodeInvalidEmail:
                        self.emailLoginStatus = "invalid email"
                    case .ErrorCodeUserDisabled:
                        self.emailLoginStatus = "User account disabled"
                    case .ErrorCodeWrongPassword:
                        self.emailLoginStatus = "Wrong Password"
                    case .ErrorCodeUserNotFound:
                        self.emailLoginStatus = "User not found"
                    case .ErrorCodeNetworkError:
                        self.emailLoginStatus = "Could not connect"
                    default:
                        self.emailLoginStatus = "Login Error"
                    }
                }
            }else{
                self.emailLoginStatus = "Logged"
            }
        })

        if self.emailLoginStatus == "Logged"{
            self.performSegueWithIdentifier("emailLoginToSearchSegue", sender: self)
        }else{
            showAlert(emailLoginStatus)
        }

}

I did debug the code step by step and this is the situation: When I tap the loginEmailButton for the first time, the email and password parameters are set to signInWithEmail function, but this function doesn't execute (the next step on debug goes directly out of function without run the completion block).

Then the simulator shows the alert message without text, but if I close the alert and tap the loginEmailButton again, the signInWithEmail is executed correctly and shows the alert with the correct message. I even tried updating the Firebase pods but the problem still remains.

Any suggestion? Thanks!


回答1:


Its asynchronous issue. signInWithEmail makes an asynchronous call to the server, meaning this call will be loaded in a different network thread, which takes some time to complete, but since your performSegueWithIdentifier is put outside the completionBlock so it get's executed even before sign In could be completed, But when you press your button next time your users had been previously been signed in from your first call, so it segues...

Just put the

self.performSegueWithIdentifier("emailLoginToSearchSegue", sender: self)

inside the signInWithEmail completionBlock().

@IBAction func loginEmailButton(sender: AnyObject) {
    FIRAuth.auth()?.signInWithEmail(email.text!, password: password.text!, completion: { (user, error) in
        if error != nil {
            if let errCode = FIRAuthErrorCode(rawValue: error!.code) {
                switch errCode {
                case .ErrorCodeInvalidEmail:
                    self.emailLoginStatus = "invalid email"
                case .ErrorCodeUserDisabled:
                    self.emailLoginStatus = "User account disabled"
                case .ErrorCodeWrongPassword:
                    self.emailLoginStatus = "Wrong Password"
                case .ErrorCodeUserNotFound:
                    self.emailLoginStatus = "User not found"
                case .ErrorCodeNetworkError:
                    self.emailLoginStatus = "Could not connect"
                default:
                    self.emailLoginStatus = "Login Error"
                }
            }
        }else{
            self.performSegueWithIdentifier("emailLoginToSearchSegue", sender: self)
        }
    })
}


来源:https://stackoverflow.com/questions/39935800/swift-firebase-function-signinwithemail-dont-execute-in-the-first-call

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