2017-12-11 11 views
-1

Swiftのコードに問題がある エラーが発生した場合は、verifyInputメソッドはfalseを返します。 何があっても常にtrueを返します。しかし、そのわずか真メソッドは変数を変更せずに返す

を返す問題がverifyInputregisterから同期的に呼び出されていることですが、それが完了ブロックとAuth.auth().createUserへの非同期呼び出しです内

@IBAction func register(_ sender: UIButton) { 

    let check = verifyInput(email :email.text! ,password: password.text!) 
    if(check==true){ 

     self.performSegue(withIdentifier: "goToAmazon", sender: nil) 

    } else if(check==false) { 

     self.message.text = "Sorry! there's an error" 
    } 


} 

func verifyInput(email: String, password: String) -> Bool { 

    var check = true 
    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in 
     if error != nil { 
      print("error") 
      check = false 
     } else if(error==nil){ 
      check = true 
      print("registered!") 
     } 

    } 

    return check 
} 
+2

は、非同期API呼び出しのいくつかの研究を行います。これを読んで – rmaddy

+0

は、あなたがよりhttps://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean –

答えて

1

を助けてください。非同期呼び出しがこれまでに完了する前に

check結果が返されています。メソッドを非同期にも変更する必要があります。漠然とこのような

何かが何をしたいです:

@IBAction func register(_ sender: UIButton) { 
    if let email = email.text, let password = password.text { 
     verifyInput(email: email, password: password) { (check) in 

      DispatchQueue.main.async { 
       // only run UI code on the main thread 
       if(check){ 
        self.performSegue(withIdentifier: "goToAmazon", sender: nil) 
       } else { 
        self.message.text = "Sorry! there's an error" 
       } 
      } 
     } 
    } 
} 

func verifyInput(email: String, password: String, escaping completion:@escaping (Bool)->Void) { 

    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in 
     if error != nil { 
      print("error") 
      completion(false) 
     } else if(error==nil){ 
      print("registered!") 
      completion(true) 
     } 
    } 
} 
+0

THNAK YOUはSO MUCHを理解するのに役立つかもしれない!!!!! – Dipie

関連する問題