0

I'm new to swift and this week I came across a problem that is already making me pluck my hair!
What happens is the following, divided in my application in several classes to leave my code more "clean" and in this wave I decided to divide some functions into classes by the project, but so far so good.
The problem is how Swift handles the execution queues, leaving behind everything that is not in my ViewController, which is hindering me.
I'll show you some of the code so you can better extend what I'm saying.
The function of the button of my ViewController:

@IBAction func btnLogin(_ sender: UIButton) {

  //The line that the Swift "skips"
  let dadosAlunoWS = FadbaWS.buscaAluno(matricula: MatriculaTbox.text!, senha: SenhaTbox.text!, token: "TOKEN")  

  //'dadosAlunoWS' aways empty because 'FadbaWS.buscaAluno' 
  //is only performed at the end of 'btnLogin'

  if dadosAlunoWS.sucesso && dadosAlunoWS.usuarioEncontrado{
      print(dadosAlunoWS.name)

  }else{ 

      //Error on request
  }        
}

I know the concept of parallelism and concurrency and researching I was able to realize that it is Swift's native to prioritize the interface, which makes everything fluid, but my problem is that my 'FadbaWS.buscaAluno' function is never executed and thus never returns a valid value .
How do I give priority so that my code only proceeds after executing this function?
How to handle the DispatchQueue in Swift?
Right now, grateful!

outrowender
  • 351
  • 1
  • 5
  • 17

3 Answers3

1

On which thread does FadbaWS.buscaAluno run? Is there any completion handler?

You mentioned FadbaWS.buscaAluno gets implemented at the end of function. This may mean it is getting run asynchronously on mainQueue. If that is the case, then you can simply perform your other tasks by dispatching them asynchronously on main queue.

DispatchQueue.main.async {
if dadosAlunoWS.sucesso && dadosAlunoWS.usuarioEncontrado{
      print(dadosAlunoWS.name)

  }else{ 

      //Error on request
  }    
}

This will work because mainQueue is essentially a serial queue and here we are assigning tasks serially on main queue. Although this will work but it would be better if you could implement a completion handler on FadbaWS.buscaAluno method.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
0

You can see how to use DispatchQueue here: How do I write dispatch_after GCD in Swift 3?

But it would be better to rewrite FadbaWS.buscaAluno() to pass in a handler which it can execute when it is ready, see: How could I create a function with a completion handler in Swift?

Community
  • 1
  • 1
simonWasHere
  • 1,307
  • 11
  • 13
0

Using a CompletionHandler was the solution, as some guys had commented here

       self.FadbaWS.buscaAluno(matricula: MatriculaTbox.text!, senha: SenhaTbox.text!, token: "TOKEN", complete:{ resultado in

          if resultado.2 && resultado.3{
            print(resultado.0)
          }else 
            //Error on request
          }
       })

In this case I needed a few parameters so I did so. It worked, but I do not know if it's the correct one

My 'buscaAluno' function:

    func buscaAluno(matricula: String, senha: String, token: String, complete: @escaping (_ nome: String?, _ login: String?, _ sucesso:Bool, _ userok:Bool) -> Void) {

       complete("Wender", "1234", true, true)

      }
    }
outrowender
  • 351
  • 1
  • 5
  • 17