0

"var CurrentStatus:status!"ステータスは列挙型です。私は変数を更新するfirebase関数を持っています。変数はfirebase関数内で更新を取得しますが、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 = "[email protected]" 
       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 

    } 
+2

、Firebasesは非同期に動作しますので、あなたはそうのような完了時に実行閉鎖パラメータを追加している、私はどうなるのか...そのようにステータスを返すことはできません。バットでは、Firebaseを値を返す関数として使用することはできません。そのようには機能しません。 Firebaseは非同期ですので、データは観測に添付されたブロック(クロージャ)でのみ利用可能です。つまり、Firebaseがデータを返す前にコードの残りの部分が実行されています。第二にあなたはselfを設定しています。ブロック内の現在のステータスは正しくなりましたが、データを返すことは、データがクロージャ内でのみ有効であるため、設定した内容を上書きしてしまうでしょう。 – Jay

答えて

1

Jayのコメントに同意します。

クラス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 = "[email protected]" 
      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) 
         } 
        }) 
       } 
      }) 
     } 
    } 
} 
関連する問題