mcpField
S
swift-concurrency-expert

swift-concurrency-expert

github.com/dimillian開発4.0k2.0k 回インストール30 週間前に更新
リポジトリ

swift-concurrency-expert について

Swift 6.2 以降のコードベースで、Swift の並行処理に関する問題をレビューして修正するスキルです。コンパイラ診断の調査から最小限の変更、ビルドとテストによる確認までを支援します。

主な機能

  1. 01コンパイラ診断と並行設定の調査
  2. 02アクター分離と Sendable の修正
  3. 03バックグラウンド処理のメインアクター分離
  4. 04再ビルドとテストによる回帰確認

活用シーン

  1. 01Swift 6.2 以降の厳格な並行処理エラーの解決
  2. 02UI 状態の安全な分離
  3. 03Sendable 警告や可変グローバル状態への対処

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/dimillian/skills --skill swift-concurrency-expert

インストール後、Claude Code で/swift-concurrency-expertを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

agents/
openai.yaml
references/
approachable-concurrency.md
swift-6-2-concurrency.md
swiftui-concurrency-tour-wwdc.md

詳細

Swift Concurrency エキスパート

概要

Swift 6.2 以降のコードベースで、アクター分離、Sendable の安全性、最新の並行処理パターンを適用し、動作への変更を最小限に抑えながら Swift Concurrency の問題を確認して修正します。

ワークフロー

1. 問題を切り分ける
  • コンパイラの診断内容と、問題のあるシンボルを正確に記録します。
  • プロジェクトの並行処理設定を確認します。Swift の言語バージョン(6.2 以降)、厳密な並行処理のレベル、および approachable concurrency(デフォルトのアクター分離/デフォルトのメインアクター分離)が有効かどうかを確認します。
  • 現在のアクターコンテキスト(@MainActor、actor、nonisolated)と、デフォルトのアクター分離モードが有効かどうかを特定します。
  • コードが UI に関連するものか、メインアクター外で実行することを意図したものかを確認します。
2. 安全性を保てる最小限の修正を適用する

データ競合への安全性を満たしながら、既存の動作を維持できる編集を優先します。

一般的な修正方法:

  • UI に関連する型:型または該当するメンバーに @MainActor を指定します。
  • メインアクター型でのプロトコル準拠:準拠を分離します(例:extension Foo: @MainActor SomeProtocol)。
  • グローバル/静的状態:@MainActor で保護するか、アクター内に移します。
  • バックグラウンド処理:負荷の高い処理を、nonisolated 型上の @concurrent 非同期関数に移すか、actor を使って可変状態を保護します。
  • Sendable のエラー:不変の値型を優先します。適切な場合にのみ Sendable 準拠を追加し、スレッド安全性を証明できない限り @unchecked Sendable は避けます。
3. 修正を検証する
  • 再ビルドし、並行処理に関する診断がすべて解消され、新たな警告が発生していないことを確認します。
  • リグレッションを確認するため、テストスイートを実行します。ビルドがクリーンでも、並行処理の変更によって微妙な実行時の問題が発生することがあります。
  • 修正によって新たな警告が発生した場合は、それぞれを新たな問題として切り分け(手順 1 に戻り)、ビルドがクリーンになりテストが通るまで反復して解決します。
例

UI に関連する型 — @MainActor の追加

// Before: data-race warning because ViewModel is accessed from the main thread
// but has no actor isolation
class ViewModel: ObservableObject {
    @Published var title: String = ""
    func load() { title = "Loaded" }
}

// After: annotate the whole type so all stored state and methods are
// automatically isolated to the main actor
@MainActor
class ViewModel: ObservableObject {
    @Published var title: String = ""
    func load() { title = "Loaded" }
}

プロトコル準拠の分離

// Before: compiler error — SomeProtocol method is nonisolated but the
// conforming type is @MainActor
@MainActor
class Foo: SomeProtocol {
    func protocolMethod() { /* accesses main-actor state */ }
}

// After: scope the conformance to @MainActor so the requirement is
// satisfied inside the correct isolation context
@MainActor
extension Foo: SomeProtocol {
    func protocolMethod() { /* safely accesses main-actor state */ }
}

@concurrent によるバックグラウンド処理

// Before: expensive computation blocks the main actor
@MainActor
func processData(_ input: [Int]) -> [Int] {
    input.map { heavyTransform($0) }   // runs on main thread
}

// After: hop off the main actor for the heavy work, then return the result
// The caller awaits the result and stays on its own actor
nonisolated func processData(_ input: [Int]) async -> [Int] {
    await Task.detached(priority: .userInitiated) {
        input.map { heavyTransform($0) }
    }.value
}

// Or, using a @concurrent async function (Swift 6.2+):
@concurrent
func processData(_ input: [Int]) async -> [Int] {
    input.map { heavyTransform($0) }
}

参考資料

  • Swift 6.2 の変更点、パターン、例については、references/swift-6-2-concurrency.md を参照してください。
  • プロジェクトで approachable concurrency モードを有効にしている場合は、references/approachable-concurrency.md を参照してください。
  • SwiftUI 固有の並行処理のガイダンスについては、references/swiftui-concurrency-tour-wwdc.md を参照してください。

コメント