1. Apa Itu Swift Async Algorithms?
1. Apa Itu Swift Async Algorithms?
Swift Async Algorithms adalah open-source package dari Apple (tersedia di GitHub: apple/swift-async-algorithms) yang menyediakan kumpulan operator untuk memproses AsyncSequence secara komposable — mirip seperti apa yang diberikan Combine untuk Publisher, tapi dibangun di atas Swift Concurrency native.
Package ini diperkenalkan bersama Swift 5.7 (WWDC 2022) dan terus berkembang. Sejak Swift 5.9, package ini dianggap production-ready dan sudah kompatibel penuh dengan Swift 6 strict concurrency. Versi 1.0 dirilis bersamaan dengan Swift 6.
Hubungan dengan AsyncSequence
AsyncSequence adalah protocol bawaan Swift yang mirip dengan Sequence biasa, tapi setiap elemen dihasilkan secara asynchronous — consumer harus await setiap elemen. Swift Async Algorithms menyediakan operator untuk mengkomposisi, mentransformasi, menggabungkan, dan membatasi laju dari satu atau lebih AsyncSequence.
AsyncSequence A ──┐
├── merge ──► debounce ──► map ──► for await in
AsyncSequence B ──┘
Perbedaan dengan Combine
| Aspek | Swift Async Algorithms | Combine |
|---|---|---|
| Dasar | AsyncSequence + Swift Concurrency |
Publisher + Reactive Streams |
| Versi minimum | iOS 16 (beberapa operator), Swift 5.7+ | iOS 13+ |
| Thread model | Cooperative thread pool Swift | GCD + scheduler |
| Back-pressure | Implicit via await |
Explicit via Demand |
| Error model | Swift typed throws | Failure type parameter |
| Cancel | Task.cancel() |
Cancellable bag |
| Swift 6 compat | Penuh | Parsial (ada warning strict concurrency) |
| Dependency | Hanya Swift stdlib | Framework Apple (bukan SPM) |