Chuyển đến nội dung
Skillpedia
Ứng dụng di động6.0k · #14451

swift-concurrency-pro

Skill này làm gì

Skill này để AI rà soát code Swift dùng async và await, tìm các lỗi có thể gây crash hoặc race condition. Skill dành cho lập trình viên Swift muốn code đồng thời an toàn hơn.

Khi nào nên dùng

  • ✓Khi bạn đang viết hoặc thay đổi code Swift dùng async và await hoặc actor.
  • ✓Khi bạn thấy cảnh báo concurrency nghiêm ngặt và cần được giải thích.
  • ✓Khi bạn muốn rà soát các bài kiểm thử async hoặc cách xử lý hủy trong Swift.

Liên kết skill

https://github.com/jacklandrin/OnlySwitch/blob/main/doc/skills/swift-concurrency-pro/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/jacklandrin/OnlySwitch/blob/main/doc/skills/swift-concurrency-pro/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Rà soát code mạng Swift của tôi xem có lỗi async nào không, và cho tôi biết cần sửa gì.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent quét các mẫu rủi ro và kiểm tra actor, task, hủy và stream theo thực hành của Swift 6.2, chỉ báo cáo các vấn đề thật.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Review Swift concurrency code for correctness, modern API usage, and adherence to project conventions. Report only genuine problems - do not nitpick or invent issues.

Review process:

  1. Scan for known-dangerous patterns using references/hotspots.md to prioritize what to inspect.
  2. Check for recent Swift 6.2 concurrency behavior using references/new-features.md.
  3. Validate actor usage for reentrancy and isolation correctness using references/actors.md.
  4. Ensure structured concurrency is preferred over unstructured where appropriate using references/structured.md.
  5. Check unstructured task usage for correctness using references/unstructured.md.
  6. Verify cancellation is handled correctly using references/cancellation.md.
  7. Validate async stream and continuation usage using references/async-streams.md.
  8. Check bridging code between sync and async worlds using references/bridging.md.
  9. Review any legacy concurrency migrations using references/interop.md.
  10. Cross-check against common failure modes using references/bug-patterns.md.
  11. If the project has strict-concurrency errors, map diagnostics to fixes using references/diagnostics.md.
  12. If reviewing tests, check async test patterns using references/testing.md.

If doing a partial review, load only the relevant reference files.

Core Instructions

  • Target Swift 6.2 or later with strict concurrency checking.
  • If code spans multiple targets or packages, compare their concurrency build settings before assuming behavior should match.
  • Prefer structured concurrency (task groups) over unstructured (Task {}).
  • Prefer Swift concurrency over Grand Central Dispatch for new code. GCD is still acceptable in low-level code, framework interop, or performance-critical synchronous work where queues and locks are the right tool – don't flag these as errors.
  • If an API offers both async/await and closure-based variants, always prefer async/await.
  • Do not introduce third-party concurrency frameworks without asking first.
  • Do not suggest @unchecked Sendable to fix compiler errors. It silences the diagnostic without fixing the underlying race. Prefer actors, value types, or sending parameters instead. The only legitimate use is for types with internal locking that are provably thread-safe.

Output Format

Organize findings by file. For each issue:

  1. State the file and relevant line(s).
  2. Name the rule being violated.
  3. Show a brief before/after code fix.

Skip files with no issues. End with a prioritized summary of the most impactful changes to make first.

Example output:

DataLoader.swift

Line 18: Actor reentrancy – state may have changed across the await.

// Before
actor Cache {
    var items: [String: Data] = [:]

    func fetch(_ key: String) async throws -> Data {
        if items[key] == nil {
            items[key] = try await download(key)
        }
        return items[key]!
    }
}

// After
actor Cache {
    var items: [String: Data] = [:]

    func fetch(_ key: String) async throws -> Data {
        if let existing = items[key] { return existing }
        let data = try await download(key)
        items[key] = data
        return data
    }
}

Line 34: Use withTaskGroup instead of creating tasks in a loop.

// Before
for url in urls {
    Task { try await fetch(url) }
}

// After
try await withThrowingTaskGroup(of: Data.self) { group in
    for url in urls {
        group.addTask { try await fetch(url) }
    }

    for try await result in group {
        process(result)
    }
}

Summary

  1. Correctness (high): Actor reentrancy bug on line 18 may cause duplicate downloads and a force-unwrap crash.
  2. Structure (medium): Unstructured tasks in loop on line 34 lose cancellation propagation.

End of example.

References

  • references/hotspots.md - Grep targets for code review: known-dangerous patterns and what to check for each.
  • references/new-features.md - Swift 6.2 changes that alter review advice: default actor isolation, isolated conformances, caller-actor async behavior, @concurrent, Task.immediate, task naming, and priority escalation.
  • references/actors.md - Actor reentrancy, shared-state annotations, global actor inference, and isolation patterns.
  • references/structured.md - Task groups over loops, discarding task groups, concurrency limits.
  • references/unstructured.md - Task vs Task.detached, when Task {} is a code smell.
  • `refere
Câu hỏi thường gặp

Skill có báo các vấn đề về phong cách nhỏ không?

Không. Skill chỉ báo các vấn đề thực sự và tránh những lời chê vặt.

Skill nhắm đến phiên bản Swift nào?

Skill nhắm đến Swift 6.2 trở lên.

Skill có giúp sửa cảnh báo concurrency không?

Có. Skill ánh xạ các lỗi concurrency nghiêm ngặt tới các cách sửa có khả năng cao.

Chi tiết

Mô tả gốc của tác giả

Reviews Swift code for concurrency correctness, modern API usage, and common async/await pitfalls. Use when reading, writing, or reviewing Swift concurrency code.

repo: jacklandrin/OnlySwitch

Độ phổ biến

Sao GitHub
6.0k
Vị trí xếp hạng
#14451
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự