2025-02-08 11:21:01
,某些文章具有时效性,若有错误或已失效,请在下方留言。Using async/await in Swift allows us to write asynchronous code that is easy to read and understand, but by itself it doesn’t enable us to run anything concurrently – even with several CPU cores working hard, async/await code would still execute sequentially.
在 Swift 中使用 async/await 可以让我们编写易于阅读和理解的异步代码,但它本身并不能让我们同时运行任何东西——即使几个 CPU 内核都在努力工作,async/await 代码仍然会按顺序执行。
To create actual concurrency – to provide the ability for multiple pieces of work to run at the same time – Swift provides us with two specific types for constructing and managing concurrency in a way that makes it easier to use: Task
and TaskGroup
. Although the types themselves aren’t complex, they unlock a lot of power and flexibility, and sit at the core of how we use concurrency with Swift.
为了创建实际的并发 – 提供同时运行多个工作的能力 – Swift 为我们提供了两种特定的类型,用于以更易于使用的方式构建和管理并发:Task
和 TaskGroup
。尽管这些类型本身并不复杂,但它们释放了大量的功能和灵活性,并且是我们如何使用 Swift 并发的核心。
Which you choose – Task
or TaskGroup
– depends on the goal of your work: if you want one or two independent pieces of work to start, then Task
is the right choice. If you want to split up one job into several concurrent operations then TaskGroup
is a better fit. Task groups work best when their individual operations return exactly the same kind of data, but with a little extra effort you can coerce them into supporting heterogenous data types.
选择哪个 – Task
或 TaskGroup
– 取决于您的工作目标:如果您希望开始一个或两个独立的工作,那么 Task
是正确的选择。如果您想将一个作业拆分为多个并发作,那么 TaskGroup
更合适。当任务组的各个作返回完全相同类型的数据时,任务组的效果最佳,但只需稍加努力,您就可以强制它们支持异构数据类型。
Although you might not realize it, you’re using tasks every time you write any async code in Swift. You see, all async functions run as part of a task whether or not we explicitly ask for it to happen. Even using async let
is syntactic sugar for creating a task then waiting for its result – special syntax that makes a particular piece of code easier to write. This is why if you use multiple sequential async let
calls they will all start executing immediately while the rest of your code continues.
尽管您可能没有意识到,但每次在 Swift 中编写任何异步代码时,您都会使用任务。你看,所有异步函数都作为任务的一部分运行,无论我们是否明确要求它发生。即使使用 async let
也是创建任务然后等待其结果的语法糖 – 使特定代码段更易于编写的特殊语法。这就是为什么如果你使用多个顺序 async let
调用,它们都将立即开始执行,而你的代码的其余部分继续。
Both Task
and TaskGroup
can be created with one of four priority levels: high
is the most important, then medium
, low
, and finally background
at the bottom. Task priorities allow the system to adjust the order in which it executes work, meaning that important work can happen before unimportant work.Task
和 TaskGroup
都可以使用以下四个优先级之一创建:high
是最重要的,然后是 medium
、low
,最后是底部的 background
。任务优先级允许系统调整其执行工作的顺序,这意味着重要工作可以在不重要的工作之前发生。
Tip: If you’ve been doing iOS programming for a while, you may prefer to use the more familiar quality of service priorities from DispatchQueue
, which are userInitiated
and utility
in place of high
and low
respectively. There is no equivalent to the old userInteractive
priority, which is now exclusively reserved for the user interface.
提示: 如果您已经进行 iOS 编程一段时间了,您可能更喜欢使用 DispatchQueue
中更熟悉的服务质量优先级,即 userInitiated
和 utility
分别代替 high
和 low
。没有与旧的 userInteractive
优先级等效的优先级,该优先级现在专门保留给用户界面。