Swift-Firebase函数signInWithEmail不在第一次调用中执行 [英] Swift - Firebase function signInWithEmail don't execute in the first call

查看:80
本文介绍了Swift-Firebase函数signInWithEmail不在第一次调用中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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)
        }

}

我确实逐步调试了代​​码,这是这种情况:当我第一次点击 loginEmailButton 时,电子邮件和密码参数设置为 signInWithEmail 函数,但此函数不会执行(调试的下一步将直接退出该函数,而无需运行完成块).

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).

然后模拟器显示不带文本的警报消息,但是如果我关闭警报并再次点击 loginEmailButton ,则会正确执行 signInWithEmail 并显示带有正确的消息.我什至尝试更新Firebase Pod,但问题仍然存在.

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.

有什么建议吗?谢谢!

推荐答案

其异步问题. signInWithEmail 对服务器进行异步调用,这意味着此调用将加载到其他网络线程中,这需要一些时间才能完成,但是由于您的 performSegueWithIdentifier 放在completionBlock之外,因此甚至可以在登录完成之前就执行该代码,但是当您下次在以前第一次登录呼叫时已登录用户时,按下按钮,它就会提示...

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...

只需放入

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

signInWithEmail completementBlock()内部.

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)
        }
    })
}

这篇关于Swift-Firebase函数signInWithEmail不在第一次调用中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆