Storyboard Segues and IBActions

I have a problem that I am running into when working with segues, storyboards, and IBActions. I have a SignUp Form with several text fields and UIButton for signing up. Basically, when the user taps sign up, the application will validate the text fields and then basically add that user to my database backend like FireBase. Also, if the signup was successful, I segue into another View Controller that says Welcome.. otherwise a UIAlert will be displayed and the segue does not happen. The problem is that even if the user didnt enter the correct info the welcome segue still happens. Here is what I've done:


1. Control Drag UIButton to WelcomeViewController and set the segue identifier to "welcome"

2. Made an IBAction method for the UIButton. See below:


I think my problem has to do with with my storyboard connection, but I'm not sure...


  @IBAction func registerPerson(_ sender: UIButton) {
      
     // BASIC CREATE USER CALL, THERE IS MORE TO THIS, BUT THIS JUST A SUMMARY OF THAT PORTION 
     createUser()
         
         // Code for server. If data was not enetered in the database, then a UIAlert is displayed
         if response.result.isFailure {
            let alert = UIAlertController(title: nil, message: "Unable to register", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
               alert.dismiss(animated: true)
            }))
            self.present(alert, animated: true)
            
         } else {

               // Perform segeue
            self.user = response.value!
            self.performSegue(withIdentifier: "welcome", sender: self)
         }
      }
   }
      
   }
  



Thanks,

I suspect you should have a synchro problem.


Where is response.result.isFailure updated ? I suppose in createUser()

-> Could you show the code ?


So, probably, when yo test


         if response.result.isFailure {


createUser has not finished and returned, probably in another thread, so isFailure is still false, hence you segue.


To avoid this, you could use semephore


You could also have createUser() to return a Bool to indicate the failure

and call

if createUser() {

} else {

}

The createUser method is from a sample application, I am still trying to dissect and understand that. I think looking into where isFailure is being updated is a good start plus the semephore idea you provided.

Storyboard Segues and IBActions
 
 
Q