Tasks and task groups共18篇
How to create and run a task 如何创建和运行任务-Stewed Noodles 资源

How to create and run a task 如何创建和运行任务

Swift’s Task struct lets us start running some work immediately, and optionally wait for the result to be returned. And it is optional: sometimes you don’t ca...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
0250
Understanding how priority escalation works 了解优先级提升的工作原理-Stewed Noodles 资源

Understanding how priority escalation works 了解优先级提升的工作原理

Every task can be created with a specific priority level, or it can inherit a priority from somewhere else. But in two specific circumstances, Swift will raise the priori...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
230
How to get a Result from a task 如何从任务中获取 Result-Stewed Noodles 资源

How to get a Result from a task 如何从任务中获取 Result

If you want to read the return value from a Task directly, you should read its value using await, or use try await if it has a throwing operation...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
210
How to cancel a Task 如何取消任务-Stewed Noodles 资源

How to cancel a Task 如何取消任务

Swift’s tasks use cooperative cancellation, which means that although we can tell a task to stop work, the task itself is free to completely ignore that instruction and carry...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
190
How to discard results in a task group 如何丢弃任务组中的结果-Stewed Noodles 资源

How to discard results in a task group 如何丢弃任务组中的结果

Swift has a special task group type called a discarding task group, which has a very specialized function: tasks that complete are automatically discarded and destroyed, ...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
150
How to voluntarily suspend a task  如何自愿暂停任务-Stewed Noodles 资源

How to voluntarily suspend a task 如何自愿暂停任务

If you’re executing a long-running task that has few if any suspension points, for example if you’re repeatedly iterating over an intensive loop, you can call Task.yield()&n...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
120
What’s the difference between async let, tasks, and task groups? async let、tasks 和 task groups 之间有什么区别?-Stewed Noodles 资源

What’s the difference between async let, tasks, and task groups? async let、tasks 和 task groups 之间有什么区别?

Swift’s async let, Task, and task groups all solve a similar problem: they allow us to create concurrency in our code so the system is able to run them efficiently. Beyo...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
120
Is it efficient to create many tasks? 创建许多任务是否有效?-Stewed Noodles 资源

Is it efficient to create many tasks? 创建许多任务是否有效?

Previously I talked about the concept of thread explosion, which is when you create many more threads than CPU cores and the system struggles to manage them effectively. Howev...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
110
What are tasks and task groups? 什么是任务和任务组?-Stewed Noodles 资源

What are tasks and task groups? 什么是任务和任务组?

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 w...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
110
How to run tasks using SwiftUI’s task() modifier 如何使用 SwiftUI 的 task() 修饰符运行任务-Stewed Noodles 资源

How to run tasks using SwiftUI’s task() modifier 如何使用 SwiftUI 的 task() 修饰符运行任务

SwiftUI provides a task() modifier that starts a new task as soon as a view appears, and automatically cancels the task when the view disappears. This is sort of the equi...
jiulinxiri的头像-Stewed Noodles 资源烩之面大会员jiulinxiri8个月前
110