Skip to content
Wen's Blog

Swift Concurrency 专题

Apr 5, 2022 — Reading

本专题主要解决 Swift 5.5 增加的 Concurrency 功能的学习,首要问题就是要明白Swift Concurrency 是什么 (opens in a new window)

推荐从官方文档学起,对接口使用能有些概念,接着从提案清单去了解,为什么提出这个功能?解决了什么问题?最终采取的方案为什么是这样做?

最后,可以看看别人对这个功能的理解,以及在实际场景下的实践经验。

1. 官方资料

Concurrency 章节文档:

WWDC 2021 视频:

所有相关提案清单如下:

2. 闲话 Swift 协程

函数式的核心,命令式的壳,单项数据流。

单项数据流可以看作者的这个系列:

  1. Redux-like state container in SwiftUI. Basics (opens in a new window)
  2. Redux-like state container in SwiftUI. Best practices (opens in a new window)
  3. Redux-like state container in SwiftUI. Container Views. (opens in a new window)
  4. Redux-like state container in SwiftUI. Connectors. (opens in a new window)
  5. Redux-like state container in SwiftUI. Swift concurrency model. (opens in a new window)

有了 Combine,还有必要存在 async/await 吗?

异步 HTTP 请求封装的实践

有了 Async/Await 和 Actors 的特性,现在在 Swift 中设计自定义 Web API 相比以往可以通过少量的抽象,使代码易于理解和扩展,文中进行了一些实践演示应用。

7. Modern Swift Concurrency from Andy Ibanez

作者 Andy Ibanez,来自玻利维亚,10+年 iOS 开发。

本系列文章是作者对 iOS 现代并发的理解。

  1. Modern Concurrency in Swift: Introduction (opens in a new window)
  2. Understanding async/await in Swift (opens in a new window)
  3. Converting closure-based code into async/await in Swift (opens in a new window)
  4. Structured Concurrency in Swift: Using async let (opens in a new window)
  5. Structured Concurrency With Group Tasks in Swift (opens in a new window)
  6. Introduction to Unstructured Concurrency in Swift (opens in a new window)
  7. Unstructured Concurrency With Detached Tasks in Swift (opens in a new window)
  8. Understanding Actors in the New Concurrency Model in Swift (opens in a new window)
  9. @MainActor and Global Actors in Swift (opens in a new window)
  10. Sharing Data Across Tasks with the @TaskLocal property wrapper in the new Swift Concurrency Model (opens in a new window)
  11. Using AsyncSequence in Swift (opens in a new window)
  12. Modern Swift Concurrency Summary, Cheatsheet, and Thanks (opens in a new window)

作者 Sundell (opens in a new window) 是一个非常勤快的 Swift 开发人员,经常在个人网站上分享关于 Swift 开发的方方面面,如果了解 Swift 的更多隐藏东西,可以关注他的网站。

9. Hacking With Swift 教程

本电子书内容主要包括传统的并发模式,GCD、Operation 等,可以复习一遍来对照 Modern Concurrency 的用法。

Learn everything you need to create safe, performant and predictable asynchronous apps by leveraging the new modern concurrency features introduced in Swift 5.5, such as async/await, tasks, actors and more.

13. 『 Swift 新并发框架 』系列文章