Firebase不会在方法内部跳转

时间:2019-08-27 03:09:51

标签: ios swift firebase google-cloud-firestore

由于某种原因,我的firebase方法所做的事情对我来说毫无意义。我想用一个用户登录,然后检查一些数据来做出决定。方法signIn()getDocument()都没有超出大括号。如果我设置一个断点或越过下一个停止点,则不在大括号内。我在做什么错了?

这是整个代码:

import Foundation
import Firebase

//@objc(LoginViewController)
class LoginViewController: UIViewController {

    @IBOutlet weak var errorMessage: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: UITextField!

    @IBAction func didTapEmailLogin(_ sender: UIButton) {

        // Check if empty
        guard emailField.text != nil, passwordField.text != nil else {
            self.errorMessage.text = "Fields can't be empty."
            return
        }

        // Log in
        let email = emailField.text!
        let password = passwordField.text!
        guard login(with: email, with: password) else {
            print("Login didn't work")
            return
        }

        // Check if user has a group yet
        guard userHasGroup() else {
            print("Getting data didn't work")
            return
        }
    }

    func userHasGroup() -> Bool {
        var succesful = true
        let db = Firestore.firestore()
        let userUid = Auth.auth().currentUser?.uid
        let docRef = db.collection("users").document(userUid!)
        docRef.getDocument { (document, _) in
            if let document = document, document.exists {
                // Test
                print(document.data() as! [String: Any])
            } else {
                succesful = false
            }
        }
        return succesful
    }

    func login(with email: String, with password: String) -> Bool {
        var succesful = true
        Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
            guard error == nil, user != nil else {
                // There was an error.
                self.errorMessage.text = "Email/password incorrect."
                succesful = false
                return
            }
        }
        return succesful
    }
}

2 个答案:

答案 0 :(得分:2)

这是异步行为的定义。第一次通过函数执行闭包之外的所有代码。然后,当此对signIn的异步调用返回时,将执行闭包内部的代码。

问题在于您的函数结构。您无法从包含闭包的函数可靠地返回值,因为该函数返回时不会设置该值。

您需要更改功能以使用完成处理程序。

我已经在Why aren't my Firebase Storage URLs uploading to Google Cloud Firestore?

上发布了一个最新示例。

答案 1 :(得分:0)

所以确实这是一个异步问题。

这就是我的工作方式:

  @IBAction func didTapEmailLogin(_ sender: UIButton) {

    // Check if empty
    guard emailField.text != nil, passwordField.text != nil else {
        self.errorMessage.text = "Fields can't be empty."
        return
    }

    let email = emailField.text!
    let password = passwordField.text!

    loginAsync(with: email, with: password) { (loginSuccesful) in
        if loginSuccesful {
            self.userHasGroupAsync(completionHandler: { (hasGroup) in
                if hasGroup {
                    self.performSegue(withIdentifier: "fromLoginToHome", sender: self)
                } else {
                    self.performSegue(withIdentifier: "fromLoginToCreateJoinGroup", sender: self)
                }
            })
        }
    }
}

func loginAsync(with email: String, with password: String, completionHandler: @escaping (Bool) -> ()) {
    var succesful = true
    Auth.auth().signIn(withEmail: email, password: password) {
        (user, error) in
        guard error == nil, user != nil else {
            // There was an error.
            self.errorMessage.text = "Email/password incorrect."
            succesful = false
            return
        }
        completionHandler(succesful)
    }
}

func userHasGroupAsync(completionHandler: @escaping (Bool) -> ()) {
    var hasGroup = false
    let db = Firestore.firestore()
    let userUid = Auth.auth().currentUser?.uid
    let docRef = db.collection("users").document(userUid!)
    docRef.getDocument { (document, _) in
        if let document = document, document.exists {
            let data: [String: Any] = document.data()!
            let group = data["group"] as! String
            if group != "" { hasGroup = true }
        }
        completionHandler(hasGroup)
    } 
}
相关问题