如何修复“警报功能”

时间:2019-07-12 13:42:15

标签: ios swift

我的项目中有一个加入 button,我想在其中输出一些警报。

第一个功能是:

func joinQuiz(id:String) -> QuizRespone? {
    var result:QuizRespone?
    let docRef = db.collection(QuizController.quizReferenceName).document(id)
    docRef.getDocument { (document, error) in
        if let document = document , document.exists {
            if DataManager.shared.userController.user.following?.contains(id) ?? false  {
                result = .joined
            } else {
                DataManager.shared.userController.user.following?.append(id)
                DataManager.shared.userController.joinQuiz(id: id)
                result = .notJoined
            }
        } else  {
            result = .badCode
        }
    }
    return result
}

按钮的操作:

@IBAction func createAction(_ sender: UIButton) {
    if titleTextField.text.isEmpty == false {
        if let result = DataManager.shared.quizController.joinQuiz(id: titleTextField.text) {
            switch result {
            case .badCode :
                self.showErrorAlert(message: "Bad code")
            case .joined:
                self.showErrorAlert(message: "You are alredy joined")
            case .notJoined:
                navigationController?.popToRootViewController(animated: true)
            }
        } 
    }
 }

我希望这会输出一些警报,但是我什么也没得到。

2 个答案:

答案 0 :(得分:0)

Firebase调用是异步的,您无法同步返回某些内容,而需要完成

func joinQuiz(id:String,completion:@escaping((QuizRespone) -> ())) { 
    let docRef = db.collection(QuizController.quizReferenceName).document(id)
    docRef.getDocument { (document, error) in
        if let document = document , document.exists {
            if DataManager.shared.userController.user.following?.contains(id) ?? false  {
                completion(.joined)
            } else {
                DataManager.shared.userController.user.following?.append(id)
                DataManager.shared.userController.joinQuiz(id: id)
                completion(.notJoined)
            }
        } else  {
            completion(.badCode)
        }
    }

}

@IBAction func createAction(_ sender: UIButton) {
    if titleTextField.text.isEmpty == false {
       DataManager.shared.quizController.joinQuiz(id: titleTextField.text) { result  in 
            switch result {
            case .badCode :
                self.showErrorAlert(message: "Bad code")
            case .joined:
                self.showErrorAlert(message: "You are alredy joined")
            case .notJoined:
                navigationController?.popToRootViewController(animated: true)
            }
        } 
    }
 }

答案 1 :(得分:0)

获取文档是异步进行的,您无法从该函数返回内容,也不能添加完成处理程序,也没有理由将QuizRespon(s)e声明为可选

func joinQuiz(id:String, completion: @escaping (QuizRespone)->Void)  {
    let docRef = db.collection(QuizController.quizReferenceName).document(id)
    docRef.getDocument { (document, error) in
        if let document = document , document.exists {
            if DataManager.shared.userController.user.following?.contains(id) ?? false  {
                completion(.joined)
            } else {
                DataManager.shared.userController.user.following?.append(id)
                DataManager.shared.userController.joinQuiz(id: id)
                completion(.notJoined)
            }
        } else  {
            completion(.badCode)
        }
    }
}

@IBAction func createAction(_ sender: UIButton) {
    if titleTextField.text.isEmpty == false {
        if let result = DataManager.shared.quizController.joinQuiz(id: titleTextField.text) { response in
            switch response {
            case .badCode :
                self.showErrorAlert(message: "Bad code")
            case .joined:
                self.showErrorAlert(message: "You are alredy joined")
            case .notJoined:
                navigationController?.popToRootViewController(animated: true)
            }
        } 
    }
 }