Swift功能停止运行?

时间:2018-08-18 13:52:30

标签: ios swift firebase firebase-authentication

我具有以下功能,尝试使用Firebase将用户登录到我的应用程序中:

func tryLogin(email: String, password: String) -> Bool {
    var loginSuccess = false

    Auth.auth().signIn(withEmail: email, password: password) { (dataResult, error) in
        if error != nil {
            print(error!)
            loginSuccess = false
        } else {
            print("logged in \(email)")
            loginSuccess = true
        }
        print("end of closure")
    }

    print("Returning -- \(loginSuccess)\n\n\n")
    return loginSuccess
}

但是,当被调用时,它似乎一直运行到打印“结束结束”为止,如果提供了正确的电子邮件和密码,则打印“已登录”结束,但是没有其他结果。 “ Returning ...”从不打印。有什么原因吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

因为它是异步的

func tryLogin(email: String, password: String,completion:@escaping(_ res:Bool) -> Void ) {

    Auth.auth().signIn(withEmail: email, password: password) { (dataResult, error) in
        if error != nil {
            print(error!)
            completion(false)
        } else {
            print("logged in \(email)")
            completion(true)
        }
        print("end of closure")
    }

}

//

致电

tryLogin(email:<#email#>,password:<#password#>) {  (res) in
  print(res)
}