1

i'm trying to get the firebase fetchProvidersForEmail to set a boolean outside of the completion closure but I am not sure how since the request is asynchronous. This is what I have:

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 is what I need to set to true if there are no errors. Any help would be appreciated.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Pikachu
  • 13
  • 7
  • Are you executing this code in another thread? If not, the completion block should catch the result, on completion. It doesn't really matter if it's async in this case. Maybe you will need to edit your else statement after `if providers != nil` to explicitly check for the result you desire. – cdslijngard Jul 06 '16 at 07:41
  • What is your problem? You want to perform this request synchronously? – kelin Jul 06 '16 at 07:54
  • Yes I would like this to behave synchronously. – Pikachu Jul 06 '16 at 08:00
  • @cdslijngard I actually do not really need a result, I just need to set segueBool to true when fetchProvidersForEmail is done executing. Right underneath it I have 'return segueBool' which does not change to true because this behaves asynchronously. – Pikachu Jul 06 '16 at 08:02

1 Answers1

0

You can set a global final/static variable with that boolean, and create a listener to that variable before the firebase callback (this will help you act like "synchronously")

This way when the variable is set you can handle the changes.

Ami Hollander
  • 2,435
  • 3
  • 29
  • 47
  • Do you happen to have a link to an example? Thanks. – Pikachu Jul 06 '16 at 18:15
  • I don't know `swift` but maybe this can help you: http://stackoverflow.com/questions/27371194/set-action-listener-programmatically-in-swift – Ami Hollander Jul 06 '16 at 18:38
  • In the first answer here you can find implementation in `Java`: http://stackoverflow.com/questions/6270132/create-a-custom-event-in-java – Ami Hollander Jul 06 '16 at 18:40
  • In short: you can create a thread when you start the callback, and inside the thread you check the variable (for example while true) and when its change you can close the thread and keep synchronously your program – Ami Hollander Jul 06 '16 at 18:48