简体   繁体   English

为什么在 swift 的 mac 命令行工具中使用 DispatchQueue.main.async 时需要运行循环?

[英]Why run loop is needed when using DispatchQueue.main.async in mac command line tool in swift?

I found Apple's document to understand why i should use run loop to implement task in main dispatch queue.我找到了 Apple 的文档来理解为什么我应该使用运行循环来实现主调度队列中的任务。

According to Apple docs ,根据苹果文档

The main dispatch queue is a globally available serial queue that executes tasks on the application's main thread.主调度队列是一个全局可用的串行队列,它在应用程序的主线程上执行任务。 This queue works with the application's run loop (if one is present) to interleave the execution of queued tasks with the execution of other event sources attached to the run loop.此队列与应用程序的运行循环(如果存在)一起使用,以将排队任务的执行与附加到运行循环的其他事件源的执行交错。 Because it runs on your application's main thread, the main queue is often used as a key synchronization point for an application.因为它在应用程序的主线程上运行,所以主队列通常用作应用程序的关键同步点。

but still, i can't understand 'why' run loop is needed.但是,我仍然无法理解“为什么”需要运行循环。 it sounds like 'it needs run loop because it needs run loop'.听起来像是“它需要运行循环,因为它需要运行循环”。 I will very appreciate it if someone explain me about this.如果有人向我解释这一点,我将不胜感激。 Thank you.谢谢你。

DispatchQueue.main.async is when you have code running on a background queue and you need a specific block of code to be executed on the main queue. DispatchQueue.main.async是当您在后台队列上运行代码并且您需要在主队列上执行特定的代码块时。

In your code, viewDidLoad is already running on the main queue so there is little reason to use DispatchQueue.main.async.在您的代码中, viewDidLoad已经在主队列上运行,因此几乎没有理由使用 DispatchQueue.main.async。

But isn't necessarily wrong to use it.但是使用它不一定是错误的。 But it does change the order of execution.但它确实改变了执行顺序。

async closure is queued up to run after the current runloop completes.在当前运行循环完成后,异步闭包排队运行。

why i should use run loop to implement task in main dispatch queue为什么我应该使用运行循环来实现主调度队列中的任务

Normally, you don't, because you are already using one!通常,您不需要,因为您已经在使用一个!

In an application project, there is a main queue run loop already .在一个应用程序项目中,已经有一个主队列运行循环。 For example, an iOS app project is actually nothing but one gigantic call to UIApplicationMain, which provides a run loop.例如,iOS 应用程序项目实际上只是对 UIApplicationMain 的一次巨大调用,它提供了一个运行循环。

That is how it is able to sit there waiting for the user to do something.这就是它能够坐在那里等待用户做某事的方式。 The run loop is, uh, running.运行循环正在运行。 And looping.和循环。

But in, say, a Mac command line tool, there is no automatic run loop.但是,比如说,在 Mac 命令行工具中,没有自动运行循环。 It runs its main function and exits immediately.它运行其主 function 并立即退出。 If you needed it not to do that, you would supply a run loop.如果您需要它不这样做,您将提供一个运行循环。

i can't understand 'why' run loop is needed我不明白“为什么”需要运行循环

Generally, a run loop is not needed for command line apps.通常,命令行应用程序不需要运行循环。 You can use run loops if you have a special need for one (eg you have some dynamic UI that is performing some tasks while you wait for user input), but the vast majority of command line apps don't require run loops.如果您有特殊需要,您可以使用运行循环(例如,您有一些在等待用户输入时正在执行某些任务的动态 UI),但绝大多数命令行应用程序不需要运行循环。

As the docs say:正如文档所说:

A run loop is an event processing loop that you use to schedule work and coordinate the receipt of incoming events.运行循环是一个事件处理循环,用于安排工作和协调接收传入事件。 The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.运行循环的目的是在有工作要做时让你的线程保持忙碌,并在没有工作时让你的线程进入睡眠状态。

So, if you need to have your app wait for some incoming events or you're dispatching tasks asynchronously between queues, then, fine, employ run loops, but otherwise, don't bother.因此,如果您需要让您的应用程序等待一些传入事件,或者您在队列之间异步调度任务,那么可以使用运行循环,否则,请不要打扰。 Most command line apps don't need to use run loops at all.大多数命令行应用程序根本不需要使用运行循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM