2016-07-06 72 views
1

我試圖讓firebase fetchProvidersForEmail在完成關閉之外設置一個布爾值,但我不確定該請求是如何異步的。這是我的:從異步調用返回布爾值的Firebase

FIRAuth.auth()?.fetchProvidersForEmail(emailSignUpTextField.text!, completion:{(providers, error) in 
     if error != nil{ 
      print(error) 

      if error!.code == FIRAuthErrorCode.ErrorCodeInvalidEmail.rawValue{ 
       self.showErrorAlert("Invalid Email", msg: "Please input a valid email.") 
      } 
     } 
     else{ 
      //If there are no errors and there are providers, the email exists 
      if providers != nil{ 
       self.showErrorAlert("Email in Use", msg: "There is already an account created with that email.") 
      } 
      //The email does not exist 
      else{ 
       self.segueBool = true   
       print("it works") 
      } 
     } 
    }) 

self.segueBool是我需要設置爲true,如果沒有錯誤。任何幫助,將不勝感激。

+0

您是否在另一個線程中執行此代碼?如果沒有,完成塊應該在完成時捕獲結果。在這種情況下它是否是異步並不重要。也許你需要在'if providers!= nil'之後編輯你的else語句來明確地檢查你想要的結果。 – cdslijngard

+0

你的問題是什麼?你想同步執行這個請求嗎? – kelin

+0

是的,我希望這表現同步。 – Pikachu

回答

0

您可以設置一個全球性的最終/靜態變量與布爾, 和火力點回調之前創建一個偵聽器變量(這將幫助你像「同步」)

這樣當變量設置你可以處理更改。

+0

你碰巧有一個例子的鏈接?謝謝。 – Pikachu

+0

我不知道'迅速',但也許這可以幫助你:http://stackoverflow.com/questions/27371194/set-action-listener-programmatically-in-swift –

+0

在第一個答案在這裏你可以找到實現'Java':http://stackoverflow.com/questions/6270132/create-a-custom-event-in-java –