简体   繁体   English

强制执行顺序-Swift3 / Xcode8-线程

[英]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.

相关问题 Swift3和Xcode8:执行功能后无法执行Segue - Swift3 and Xcode8: Not able to perform segue after execution of a function iOS Swift3 XCode8 CoreData - ios swift3 xcode8 coredata UIImagePickerController 使应用程序崩溃 | Swift3,Xcode8 - UIImagePickerController crashes app | Swift3, Xcode8 UIImageView被隐藏在Xcode8和Swift3中 - UIImageView gets hidden in Xcode8 and Swift3 Xcode8:为什么 Xcode8 在转换为 Swift3 语法后没有构建/编译 - Xcode8 : Why Xcode8 is not building/compiling after converting to Swift3 syntax's 将 CMTime 保存在核心数据 + Xcode8 + swift3 - save CMTime in core data + Xcode8 + swift3 urlSession:dataTask:didReceive:在Xcode8 Swift3中没有调用completionHandler - urlSession:dataTask:didReceive:completionHandler not called in Xcode8 Swift3 用于捕获QR代码的代码在Xcode8和Swift3中似乎无法正常工作 - Code for capturing QR Code doesn't seem working well in Xcode8 and Swift3 XCode8 Swift3 - 打开联系人列表并通过单击检索数据的问题 - XCode8 Swift3 - Problems with open contact list and retrieve data by click 下拉刷新时,UITableview中发生错误,所有数据重复自身。 使用Swift3,Xcode8可以帮助任何人吗 - Error in UITableview when pull down to refresh, all data repeating itself. Can any one help using Swift3 , Xcode8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM