スキル一覧に戻る
fusengine

swift-concurrency

by fusengine

Redefining development through cognitive automation and collaborative agent systems.

0🍴 0📅 2026年1月24日
GitHubで見るManusで実行

SKILL.md


name: swift-concurrency description: Implement Swift 6 concurrency with async/await, actors, Sendable, Task, MainActor. Use when handling asynchronous operations, managing shared state safely, fixing data race errors, or migrating to Swift 6 strict concurrency. user-invocable: false

Swift 6 Concurrency

Async/Await Basics

// Async function
func fetchUser(id: String) async throws -> User {
    let (data, _) = try await URLSession.shared.data(from: url)
    return try JSONDecoder().decode(User.self, from: data)
}

// Parallel execution
async let users = fetchUsers()
async let posts = fetchPosts()
let (loadedUsers, loadedPosts) = try await (users, posts)

// Task with cancellation
func loadData() {
    task?.cancel()
    task = Task {
        try Task.checkCancellation()
        let data = try await api.fetch()
        guard !Task.isCancelled else { return }
        await MainActor.run { self.items = data }
    }
}

Actors for Thread Safety

actor DataCache {
    private var storage: [String: Data] = [:]

    func get(_ key: String) -> Data? { storage[key] }
    func set(_ key: String, data: Data) { storage[key] = data }
    func clear() { storage.removeAll() }
}

// Usage - requires await
let cache = DataCache()
await cache.set("user", data: userData)
let data = await cache.get("user")

@MainActor for UI

@MainActor
final class ViewModel: ObservableObject {
    @Published var items: [Item] = []
    @Published var isLoading = false

    func refresh() async {
        isLoading = true
        defer { isLoading = false }
        items = await repository.fetchItems()
    }
}

// Or mark specific function
func updateUI() async {
    await MainActor.run {
        self.label.text = "Updated"
    }
}

Sendable Protocol

// Value types - automatic
struct User: Sendable {
    let id: UUID
    let name: String
}

// Reference types - must be immutable + final
final class Config: Sendable {
    let apiKey: String
    init(apiKey: String) { self.apiKey = apiKey }
}

// @unchecked when you manage safety manually
final class ThreadSafeCache: @unchecked Sendable {
    private let lock = NSLock()
    private var storage: [String: Any] = [:]
}

// Closures
func process(completion: @Sendable @escaping () -> Void) {
    Task { completion() }
}

Common Fixes for Swift 6

ErrorSolution
"not Sendable"Make struct Sendable or use actor
"actor-isolated"Add await or use nonisolated
"main actor-isolated"Use @MainActor or MainActor.run
"capture of mutable"Use let or capture in Task

Task Groups

func fetchAllUsers(ids: [String]) async throws -> [User] {
    try await withThrowingTaskGroup(of: User.self) { group in
        for id in ids {
            group.addTask { try await fetchUser(id: id) }
        }
        return try await group.reduce(into: []) { $0.append($1) }
    }
}

スコア

総合スコア

60/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です