0

Im trying to update a variable inside a class "var CurrentStatus:status! " status is an enum. I have a firebase function that will update the variable. the variable get update inside the firebase function but it will not update the variable outside of the firebase function

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

    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
SwiftER
  • 1,235
  • 4
  • 17
  • 40
  • 2
    Well, right off the bat, you cannot use Firebase as a function to return a value. It doesn't work that way. Firebase is asynchronous so the data would only be available with the block (closure) attached to the observe. In other words - the rest of your code is executing before Firebase has returned it's data. Secondly you are setting self.Current status within the block correctly, but returning it will probably overwrite what it was set to, again because the data is only valid within the closure. – Jay May 16 '16 at 18:34

1 Answers1

1

Agreed with Jay's comment. You cannot return the status like that because Firebases works asynchronously... what I would do, is add a closure parameter that executions on completion like so:

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)
                        }
                    })
                }
            })
        }
    }
}
JJJ
  • 2,889
  • 3
  • 25
  • 43