SKILL

swift-concurrency-6-2

Swift 6.2 Approachable Concurrency: single-threaded by default, @concurrent for explicit background offloading, and isolated conformances for main-actor types.

Translation reviewed
Read the original Chinese text

Swift 6.2 可接近的并发性 — 默认单线程,@concurrent 用于显式后台卸载,隔离一致性用于主 actor 类型。

Target: Target not categorized. Automatically collected · unverified.

Excerpt from the author’s documentation

Extracted from a fixed version of SKILL.md in its original language. This is the author’s documentation; installation, compatibility and runtime results have not been verified by this site.

Read the original Chinese text

Topics in the source

  • Swift 6.2 可接近的并发
  • 何时启用
  • 核心问题:隐式的后台卸载
  • 核心模式 — 隔离的一致性
  • 核心模式 — 全局和静态变量
  • MainActor 默认推断模式
  • 核心模式 — 使用 @concurrent 进行后台工作
  • 关键设计决策

采用 Swift 6.2 并发模型的模式,其中代码默认在单线程上运行,并发是显式引入的。在无需牺牲性能的情况下消除常见的数据竞争错误。 将 Swift 5.x 或 6.0/6.1 项目迁移到 Swift 6.2 解决数据竞争安全编译器错误 设计基于 MainActor 的应用架构 将 CPU 密集型工作卸载到后台线程 在 MainActor 隔离的类型上实现协议一致性 在 Xcode 26 中启用“可接近的并发”构建设置 在 Swift 6.1 及更早版本中,异步函数可能会被隐式卸载到后台线程,即使在看似安全的代码中也会导致数据竞争错误: Swift 6.2 修复了这个问题:异步函数默认保持在调用者所在的 actor 上。 MainActor 类型现在可以安全地符合非隔离协议: 编译器确保该一致性仅在主 actor 上使用: 使用 MainActor 保护全局/静态状态: Swift 6.2 引入了一种模式,默认推断 MainActor — 无需手动标注: 此模式是选择启用的,推荐用于应用、脚本和其他可执行目标。 当需要真正的并行性时,使用 @concurrent 显式卸载: 重要: 此示例需要启用“可接近的并发”构建设置 — SE-0466 (MainActor 默认隔离) 和 SE-0461 (默认非隔离非发送)。启用这些设置后,extractSticker 会保持在调用者所在的 actor 上,使得可变状态的访问变得安全。如果没有这些设置,此代码存在数据竞争 — 编译器会标记它。 要使用 @concurrent: 1. 将包含类型标记为 nonisolated 2. 向函数添加 @concurrent 3. 如果函数还不是异步的,则添加 async 4. 在调用点添加 await | 决策 | 原理 | |----------|-----------| | 默认单线程 | 最自然的代码是无数据竞争的;并发是选择启用的 | | 异步函数保持在调用者所在的 actor 上 | 消除了导致数据竞争错误的隐式卸载 | | 隔离的一致性 | MainActor 类型可以符合协议,而无需不安全的变通方法 | | @concurrent 显式选择启用 | 后台执行是一种有意的性能选择,而非偶然 | | MainActor 默认推断 | 减少了应用目标中样板化的 @MainActor 标注 | | 选择启用采用 | 非破坏性的迁移路径 — 逐步启用功能 | 1. 在 Xcode 中启用:构建设置中的 Swift Compiler > Concurrency 部分 2. 在 SPM 中启用:在包清单中使用 SwiftSettings API 3. 使用迁移工具:通过 swift.org/migration 进行自动代码更改 4. 从 MainActor 默认值开始:为应

This is an excerpt. Refer to the complete source and its current requirements.

Source repository
affaan-m/ECC
Source file
docs/zh-CN/skills/swift-concurrency-6-2/SKILL.md
Fixed revision
d2ef2680594f9a17526a6e1a2e18b3d242c5d17b
Read the complete original documentation ↗

Sources and evidence

This record was collected automatically. Its documentation, suitability and runtime behavior have not been manually verified.

Author: affaan-m/ECC · original repository metadata. Documentation review: Not reviewed. Last source check: 2026-11-09T15:01:42.651Z.

Categories are inferred from names, descriptions and paths. They do not establish compatibility.

License and attribution

MIT. Third-party content and copyright remain with their authors. Refer to the original license and terms.

Original license ↗

Synchronization, copying and user-reported success do not mark a resource as runtime-tested.

Limitations and notes

  • Collecting metadata does not mean this site has installed, adapted, runtime-tested or security-reviewed the resource.