16. Ringkasan & Decision Guide
16. Ringkasan & Decision Guide
Tabel Decision Guide
| Situasi | Tool yang Tepat |
|---|---|
| Shared mutable state diakses dari banyak Task | actor |
| Main-thread only UI state | @MainActor |
| Simple counter/flag yang diakses concurrent | Atomic<T> |
| Complex state yang butuh compound operations | Mutex<T> |
| Value type yang harus crossing isolation boundary | Conformance Sendable |
| Non-Sendable yang harus dipindah ke actor | sending parameter |
| Library C/ObjC yang sudah thread-safe tapi bukan Sendable | @unchecked Sendable |
| Gradual migration dari Swift 5 ke Swift 6 | @preconcurrency |
Verifikasi runtime untuk @unchecked Sendable |
Thread Sanitizer |
| Project baru dari awal | Swift 6 mode + strict concurrency |
Checklist Data Race Safety
Compile-time:
[ ] Aktifkan -strict-concurrency=complete (atau Swift 6 mode)
[ ] Semua mutable shared state ada di actor atau Mutex
[ ] Semua value yang crossing isolation boundary adalah Sendable
[ ] Tidak ada @unchecked Sendable tanpa justifikasi dan komentar
[ ] @MainActor di semua class yang main-thread only
Runtime:
[ ] Thread Sanitizer aktif di CI runs
[ ] Stress test concurrent operations di test suite
[ ] Tidak ada DispatchQueue.sync dari async context (deadlock risk)
Design:
[ ] Actor re-entrancy sudah dipertimbangkan di semua suspend points
[ ] Tidak ada lock yang dibawa melewati await
[ ] Granularitas actor sesuai — tidak terlalu kasar, tidak terlalu halus
Penutup
Data race safety bukan sekadar feature — ini adalah paradigma baru dalam menulis concurrent Swift code. Dengan Swift 6, program yang compile adalah program yang bebas data race secara guaranteed (untuk kode yang tidak menggunakan escape hatches seperti @unchecked Sendable atau nonisolated(unsafe)).
Mulai adopsi dengan mengaktifkan -strict-concurrency=targeted, konversi class ke actor satu per satu, lalu naikkan ke complete setelah semua actor dan Sendable conformances beres. Timeline yang realistis: 1-2 sprint untuk codebase medium (50k-100k LOC).
Langkah berikutnya: pelajari swift-actor-sendable-deep-dive.md untuk detail tentang executor internals dan hop cost, serta swift-isolation-deep-dive.md untuk edge cases di protocol conformance dan closure capture.