Skip to content

A small iOS SwiftUI app showing how to use the AsyncStream.Continuation → AsyncStream pattern in Swift Concurrency

Notifications You must be signed in to change notification settings

banghuazhao/AsyncStreamPatternDemoApp

Repository files navigation

AsyncStreamPatternDemo

A small iOS SwiftUI app showing how to use the AsyncStream.Continuation → AsyncStream pattern in Swift Concurrency:

  • Networking: Bridge URLSession callbacks into AsyncStream using AsyncStream.Continuation.
  • Location: Bridge CLLocationManagerDelegate into AsyncStream using AsyncStream.Continuation.
  • NotificationCenter: Bridge notifications to an AsyncStream via AsyncStream.Continuation.
  • CADisplayLink: Bridge frame ticks to an AsyncStream via AsyncStream.Continuation.
  • Reachability (NWPathMonitor): Bridge network path updates to an AsyncStream.
  • UIControl (UITextField): Bridge target–action editingChanged to an AsyncStream.

Screenshots

Demo

Run

  • Open AsyncStreamPatternDemoApp.xcodeproj in Xcode 16+.
  • Select the AsyncStreamPatternDemoApp scheme and a simulator/device, then press Cmd+R.
  • When prompted, allow Location for the demo.

Notes: Networking uses https://jsonplaceholder.typicode.com/todos/1. DisplayLink runs on the main runloop. Reachability uses NWPathMonitor.

Why This Matters

This pattern is the async/await equivalent of the Combine Subject → Publisher pattern:

  • Convert delegate-style APIs to AsyncStreams
  • Drive SwiftUI UIs with async state using @Observable
  • Decouple imperative event sources from async/await subscribers
  • Modern Swift Concurrency approach without third-party frameworks

Architecture

  • SwiftUI + MVVM
  • Swift Concurrency (async/await + AsyncStream)
  • @Observable and @MainActor for ViewModels
  • No third-party dependencies

Comparison with Combine

Combine Swift Concurrency
PassthroughSubject AsyncStream.Continuation (stateless)
CurrentValueSubject AsyncStream.Continuation (with stored value)
Publisher AsyncStream
.sink { } / .assign for await value in stream

Key Concepts

  1. AsyncStream.Continuation is the bridge between imperative and async/await programming
  2. Use continuation.yield() to send values into the stream
  3. Use continuation.finish() to complete the stream
  4. Handle cleanup with continuation.onTermination
  5. Use @MainActor and @Observable for SwiftUI integration

💡 Author: Banghua Zhao
⭐️ Please star if you find this useful!

About

A small iOS SwiftUI app showing how to use the AsyncStream.Continuation → AsyncStream pattern in Swift Concurrency

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages