0
func passcodeViewController(_ passcodeViewController: TOPasscodeViewController, isCorrectCode code: String) -> Bool {

        let userDefault = UserDefaults.standard
        let tokenPinCode = userDefault.string(forKey: "tokenPinCode")
        let mailData = self.emailField.text
        let dataStruct = mailData!+"|"+tokenPinCode!
        print("1")
        self.checkToken(code: dataStruct) { (response) in
            if(response[0] == "OK"){
                print("2")
                self.alertPasswordChange(text: "Podaj nowe hasło", code: dataStruct)
            }else{
                self.standardAlert(title: "Znaleziono błędy", message: "Podany kod jest błedny", ok: "Rozumiem")
                self.werifyButton.isEnabled = true
            }
        }
        print("3")
        return false
    }

Function returns: Print -> 1 -> 3 -> 2

How to get the effect to work out: Print -> 1 -> 2 -> 3

3 Answers3

1

Make your function void and pass completion handler which can handle bool value.

func passcodeViewController(_ controller: Controller, code: String, @escaping handler: (Bool) -> ()) {
  // Your logic
  asyncRequest(...) {
    response in
    let result = ... // find whether code ok
    handler(result)
  }
}

You can call this like:

passcodeViewController(controller, code: "$&36_$") {
  (isOk: Bool) in
  print(3)
  print("code is ok: \(isOk)")
}
Konstantin Berkov
  • 1,193
  • 3
  • 14
  • 27
0

You can implement this using semaphore. Semaphore provide the functionality to wait until your function completed. Look at following code.

let serialQueue = DispatchQueue(label: "co.random.queue")

func funcA() {
    print("Print A")

    let semaphore = DispatchSemaphore(value: 1)

    serialQueue.async {
        print("Print B")
        semaphore.signal()
    }
    semaphore.wait()

    print("Print C")
}

funcA()

The above code is execute with async function, I have implemented in playground and it print following output;

Print A
Print B
Print C

I remove semaphore then print as:

Print A
Print C
Print B

I hope this will work for you.

Sagar Chauhan
  • 5,715
  • 2
  • 22
  • 56
-1

Make your function void and pass completion handler with signature

Konstantin Berkov
  • 1,193
  • 3
  • 14
  • 27