为什么在 swift 中使用 mac 命令行工具中的 DispatchQueue.main.async 时需要 运行 循环?
Why run loop is needed when using DispatchQueue.main.async in mac command line tool in swift?
我找到了 Apple 的文档来理解为什么我应该使用 运行 循环来实现主调度队列中的任务。
根据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.
但是,我仍然无法理解需要 'why' 运行 循环。听起来像 'it needs run loop because it needs run loop'。如果有人向我解释这件事,我将不胜感激。谢谢。
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.
In your code, viewDidLoad
is already running on the main queue so
there is little reason to use DispatchQueue.main.async.
But isn't necessarily wrong to use it. But it does change the order of
execution.
当前 运行 循环完成后,异步闭包排队到 运行。
why i should use run loop to implement task in main dispatch queue
通常情况下,你不需要,因为你已经在使用了!
在一个应用项目中,已经有一个主队列运行循环已经。例如,一个 iOS 应用程序项目实际上只是对 UIApplicationMain 的一次巨大调用,它提供了一个 运行 循环。
这就是它能够坐在那里等待用户做某事的原因。 运行 循环是,呃,运行ning。并循环播放。
但是,比方说,在 Mac 命令行工具中,没有自动 运行 循环。它 运行 是其主要功能并立即退出。如果您需要它不这样做,您可以提供一个 运行 循环。
i can't understand 'why' run loop is needed
一般来说,命令行应用程序不需要 运行 循环。如果您有特殊需要,可以使用 运行 循环(例如,您有一些动态 UI 在等待用户输入时执行某些任务),但绝大多数命令行应用程序不不需要 运行 个循环。
正如the docs所说:
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.
因此,如果您需要让您的应用等待某些传入事件,或者您在队列之间异步分派任务,那么可以使用 运行 循环,否则,请不要打扰。大多数命令行应用程序根本不需要使用 运行 循环。
我找到了 Apple 的文档来理解为什么我应该使用 运行 循环来实现主调度队列中的任务。
根据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.
但是,我仍然无法理解需要 'why' 运行 循环。听起来像 'it needs run loop because it needs run loop'。如果有人向我解释这件事,我将不胜感激。谢谢。
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.In your code,
viewDidLoad
is already running on the main queue so there is little reason to use DispatchQueue.main.async.But isn't necessarily wrong to use it. But it does change the order of execution.
当前 运行 循环完成后,异步闭包排队到 运行。
why i should use run loop to implement task in main dispatch queue
通常情况下,你不需要,因为你已经在使用了!
在一个应用项目中,已经有一个主队列运行循环已经。例如,一个 iOS 应用程序项目实际上只是对 UIApplicationMain 的一次巨大调用,它提供了一个 运行 循环。
这就是它能够坐在那里等待用户做某事的原因。 运行 循环是,呃,运行ning。并循环播放。
但是,比方说,在 Mac 命令行工具中,没有自动 运行 循环。它 运行 是其主要功能并立即退出。如果您需要它不这样做,您可以提供一个 运行 循环。
i can't understand 'why' run loop is needed
一般来说,命令行应用程序不需要 运行 循环。如果您有特殊需要,可以使用 运行 循环(例如,您有一些动态 UI 在等待用户输入时执行某些任务),但绝大多数命令行应用程序不不需要 运行 个循环。
正如the docs所说:
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.
因此,如果您需要让您的应用等待某些传入事件,或者您在队列之间异步分派任务,那么可以使用 运行 循环,否则,请不要打扰。大多数命令行应用程序根本不需要使用 运行 循环。