小编典典

我如何在firebase功能之外访问firebase变量

swift

我试图更新类“ var
CurrentStatus:status!”内的变量,状态是一个枚举。我有一个firebase函数,它将更新变量。变量在firebase函数内部获取更新,但不会在firebase函数外部更新变量

class signUpClass:UIViewController {

    // check to see if form is empty

    let ihelpController = UIViewController()
    var CurrentStatus:status!

    func signUp(var formArray: [String:String]) -> status{

        var formStatus:status = ihelpController.checkIfFormIsEmpty(formArray)

        if (formStatus == status.success){
          //form is ok to process
          // check DOB
          //TODO: create date calculation function

            let DateOfBirth:Int = 18

            if DateOfBirth < 18 {
               //user is not 18 they can not register
                alertError("oops", message: "You must be 18 to register", comfirm: "Ok")

            } else {
                //Proceed with registration
                let firebaseController = Firebase()
                var email = "asdf@afd.com"
                var password = "1234"

                firebaseController.refPath("users").createUser(email, password: password, withValueCompletionBlock: {error, result in

                    if error != nil {
                        print("registration Error")
                      self.alertError("oops", message: "That email is registered already", comfirm: "OK")

                    } else {
                        let vc =
                        print("user can register")
                        firebaseController.firebaseRefUrl().authUser(email, password: password, withCompletionBlock:{
                            error, authdata in

                            if error != nil {

                                print("login Error")
                            }else{

                                let userId = firebaseController.firebaseRefUrl().authData.uid

                                formArray["userId"] = userId

                                firebaseController.refPath("users/\(userId)").updateChildValues(formArray)
                                print("user is register and can proceed to dashBoard")

                                //Proceed to dashboard
                                self.CurrentStatus = status.success
                            }

                        })

                    }
                })

            }




        }
       return CurrentStatus

    }

阅读 318

收藏
2020-07-07

共1个答案

小编典典

同意杰伊的评论。您无法返回那样的状态,因为Firebases异步工作…我要做的是添加一个闭包参数,该参数在完成时执行,如下所示:

class signUpClass:UIViewController {

// check to see if form is empty

let ihelpController = UIViewController()
var CurrentStatus:status!

func signUp(var formArray: [String:String], complete:(CurrentStatus)->()){

    var formStatus:status = ihelpController.checkIfFormIsEmpty(formArray)

    if (formStatus == status.success){
      //form is ok to process
      // check DOB
      //TODO: create date calculation function

        let DateOfBirth:Int = 18

        if DateOfBirth < 18 {
           //user is not 18 they can not register
            alertError("oops", message: "You must be 18 to register", comfirm: "Ok")

        } else {
            //Proceed with registration
            let firebaseController = Firebase()
            var email = "asdf@afd.com"
            var password = "1234"

            firebaseController.refPath("users").createUser(email, password: password, withValueCompletionBlock: {error, result in

                if error != nil {
                    print("registration Error")
                  self.alertError("oops", message: "That email is registered already", comfirm: "OK")

                } else {
                    let vc =
                    print("user can register")
                    firebaseController.firebaseRefUrl().authUser(email, password: password, withCompletionBlock:{
                        error, authdata in

                        if error != nil {

                            print("login Error")
                        }else{

                            let userId = firebaseController.firebaseRefUrl().authData.uid

                            formArray["userId"] = userId

                            firebaseController.refPath("users/\(userId)").updateChildValues(formArray)
                            print("user is register and can proceed to dashBoard")

                            //Send status to callback to handle
                            complete(status.success)
                        }
                    })
                }
            })
        }
    }
}
2020-07-07