Andoni Da Silva
Andoni Da Silva

Reputation: 1341

Swift 4 local variable value assignment

I am trying to recovery a value from firebase database and compare it with a UITextField value, in case of matching, I save it to a var that I will us. The problem is that the variable in question has a default value just when I use it.

Above I show my func code where the variable affected is "codeRecovered":

     @IBAction func signUpAction(_ sender: AnyObject)

    {
        var codeRecovered: String = ""
        if emailSignUpTextField.text == "" || self.secretCodeTextField.text == "" {

            let alertController = UIAlertController(title: "Error", message: "Please enter your email, pin code and password", preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)
            present(alertController, animated: true, completion: nil)

        } else {

            self.dbHandler = self.ref?.child("Companies").observe(.value, with: { (snapshot) in
                for child in snapshot.children {
                    let snap = child as! DataSnapshot
                    let value = snap.value as! [String:String]
                    if let auxSecretCode = value["secretCode"]
                    {
                        if auxSecretCode == self.secretCodeTextField.text{
                            print("Value recovered OK(works fine): \(auxSecretCode)")
                            codeRecovered = auxSecretCode
                            print("Recovered value saved OK(works fine): \(codeRecovered)")
                        }
                    }

                }

            })
    //Here codeRecovered is already ""
                print("\(codeRecovered) is the recovered value(empty) and \(self.secretCodeTextField.text ?? "def") is the textField value")
                if codeRecovered != self.secretCodeTextField.text{
                    let alertController = UIAlertController(title: "Error", message: "Please enter a correct pin code", preferredStyle: .alert)

                    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)
                    present(alertController, animated: true, completion: nil)

                }
 //....

Upvotes: 0

Views: 188

Answers (1)

GIJOW
GIJOW

Reputation: 2353

Async calls with sync result use....

 @IBAction func signUpAction(_ sender: AnyObject)

{
    var codeRecovered: String = ""
    if emailSignUpTextField.text == "" || self.secretCodeTextField.text == "" {

        let alertController = UIAlertController(title: "Error", message: "Please enter your email, pin code and password", preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)

    } else {

        self.dbHandler = self.ref?.child("Companies").observe(.value, with: { (snapshot) in
            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let value = snap.value as! [String:String]
                if let auxSecretCode = value["secretCode"]
                {
                    if auxSecretCode == self.secretCodeTextField.text{
                        print("Value recovered OK(works fine): \(auxSecretCode)")
                        codeRecovered = auxSecretCode
                        print("Recovered value saved OK(works fine): \(codeRecovered)")
                    }
                }

            }

//Here codeRecovered is already ""
            print("\(codeRecovered) is the recovered value(empty) and \(self.secretCodeTextField.text ?? "def") is the textField value")
            if codeRecovered != self.secretCodeTextField.text{
                let alertController = UIAlertController(title: "Error", message: "Please enter a correct pin code", preferredStyle: .alert)

                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)
                present(alertController, animated: true, completion: nil)

            }                

        })

to use your codeRecovered in a sequence it must be within self.dbHandler = self.ref?.child("Companies").... block because it runs in async thread

Upvotes: 3

Related Questions