12. Ringkasan & Quick Reference
12. Ringkasan & Quick Reference
API Quick Reference
| API | Kegunaan | Min. Swift | Min. iOS |
|---|---|---|---|
async/await |
Fungsi asinkron linear | 5.5 | 15 |
Task {} |
Jalankan async dari sync context | 5.5 | 15 |
Task.detached {} |
Async tanpa mewarisi isolation | 5.5 | 15 |
async let |
Concurrent binding, paralel | 5.5 | 15 |
withTaskGroup |
Concurrency dinamis | 5.5 | 15 |
actor |
Isolation state custom | 5.5 | 15 |
@MainActor |
Isolation ke main thread | 5.5 | 15 |
nonisolated |
Bebas dari actor isolation | 5.5 | 15 |
Sendable |
Marker keamanan lintas actor | 5.5 | 15 |
@unchecked Sendable |
Opt-out safety check | 5.5 | 15 |
withCheckedContinuation |
Bridge callback → async (safe) | 5.5 | 15 |
AsyncStream |
Custom async sequence | 5.5 | 15 |
@preconcurrency |
Suppress warning library lama | 5.6 | 15 |
@Observable |
Observation framework | 5.9 | 17 |
Decision Guide: Pilih yang Mana?
Butuh menjalankan async dari UIKit action / lifecycle?
→ Task { await ... } (mewarisi @MainActor dari ViewController)
Butuh beberapa async operasi berjalan paralel?
→ async let (jika jumlah fix) atau withTaskGroup (jika dinamis)
Butuh melindungi state dari multiple thread?
→ actor (state yang perlu sequential access)
→ @MainActor class (state yang harus di main thread)
Butuh bridge dari callback/delegate ke async?
→ withCheckedContinuation (single callback)
→ AsyncStream (callback berulang / delegate)
Butuh data model yang aman dikirim lintas actor?
→ struct: Sendable (preferred, immutable)
→ @unchecked Sendable (class dengan manual locking, as last resort)
Library pihak ketiga menghasilkan Sendable warning?
→ @preconcurrency import LibraryName
Checklist Migrasi Bertahap
Persiapan:
- Minimal iOS 15 (atau gunakan
@available(iOS 15, *)) - Aktifkan
-warn-concurrencydi build flags - Identifikasi semua GCD, DispatchGroup, completion handler
Layer Worker/Service:
- Ganti callback menjadi
async throws - Model data tambahkan
: Sendable - Wrap 3rd party callback dengan
withCheckedContinuation
Layer Interactor/UseCase:
- Ubah ke
actor(jika ada state) atau keep sebagaistruct(jika stateless) - Ganti
DispatchGroupdenganasync let/withTaskGroup - Gunakan
await presenter?.method()langsung — Swift auto-hop ke@MainActorvia protocol
Layer ViewController:
- Tambahkan
@MainActordi class declaration - Hapus
DispatchQueue.main.async {} - Ganti completion handler dengan
Task { await ... } - Simpan
Taskreference untuk cancellation
Menuju Swift 6:
- Upgrade
-strict-concurrency=complete - Fix semua warning yang tersisa
- Enable Swift 6 mode per-target
- Hapus
@preconcurrencyyang sudah tidak diperlukan
Dibuat: 2026-05-12 | Swift 5.5–5.10 | iOS 15+ | Panduan Adopsi Bertahap Swift Concurrency