[英]Force execution order - Swift3/Xcode8 - Threads
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. 问题是Swift如何处理执行队列,而留下了ViewController之外的所有内容,这阻碍了我。
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: 我的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 . 我知道并行性和并发性的概念,并且通过研究,我能够意识到对接口进行优先级排序是Swift的本机,这使一切变得流畅,但是我的问题是我的'FadbaWS.buscaAluno'函数从未执行过,因此永远不会返回有效值。
How do I give priority so that my code only proceeds after executing this function? 如何赋予优先级,以便我的代码仅在执行此功能后才能继续执行?
How to handle the DispatchQueue in Swift? 如何在Swift中处理DispatchQueue?
Right now, grateful! 现在,感激不尽!
You can see how to use DispatchQueue here: How do I write dispatch_after GCD in Swift 3? 您可以在此处查看如何使用DispatchQueue: 如何在Swift 3中编写GCD的dispatch_after?
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? 但是最好重写FadbaWS.buscaAluno()来传递一个处理程序,该处理程序在就绪时可以执行,请参见: 如何在Swift中使用完成处理程序创建函数?
On which thread does FadbaWS.buscaAluno
run? FadbaWS.buscaAluno
在哪个线程上运行? Is there any completion handler? 有没有完成处理程序?
You mentioned FadbaWS.buscaAluno
gets implemented at the end of function. 您提到FadbaWS.buscaAluno
在函数结尾处实现。 This may mean it is getting run asynchronously on mainQueue. 这可能意味着它正在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. 这将起作用,因为mainQueue本质上是一个串行队列,并且这里我们在主队列上串行分配任务。 Although this will work but it would be better if you could implement a completion handler on FadbaWS.buscaAluno
method. 虽然可以,但是如果可以在FadbaWS.buscaAluno
方法上实现完成处理程序, FadbaWS.buscaAluno
更好了。
Using a CompletionHandler was the solution, as some guys had commented here 正如一些人在这里评论的那样,使用CompletionHandler是解决方案
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: 我的“ buscaAluno”功能:
func buscaAluno(matricula: String, senha: String, token: String, complete: @escaping (_ nome: String?, _ login: String?, _ sucesso:Bool, _ userok:Bool) -> Void) {
complete("Wender", "1234", true, true)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.