10.12 · AsyncStream buffering, continuation misuse and notifications as async sequences
Bridging old callback APIs into async code is where concurrency bugs hide: a continuation resumed twice or never, a stream that drops or buffers events you did not expect.
Mental model. A continuation must be resumed exactly once; a stream must say what to do when the consumer is slower than the producer.
Streams and continuations
Buffering policy, resume-once rules and notification sequences.
- What does an AsyncStream do when the producer yields faster than the consumer reads, and how do the buffering policies differ?
An AsyncStream is a queue between a producer (the Continuation you yield into) and a consumer (for await). When the consumer is slower, the stream has to… - What happens when a continuation is resumed twice or never, and how do you wrap a callback API correctly?
A continuation turns a callback into a suspension point, and the contract is resume exactly once. With withCheckedContinuation the runtime enforces it.… - How do you consume NotificationCenter, delegates and other repeated callbacks as async sequences, and what are the pitfalls?
NotificationCenter.default.notifications(named:) returns an AsyncSequence of notifications: a for await loop replaces the observer token and the cleanup, and…
More in Concurrency
- 10.1 · Queues, QoS, barriers, groups, semaphores and sources
- 10.2 · Operation, OperationQueue, dependencies and cancellation
- 10.3 · async/await, tasks, task groups, cancellation and task locals
- 10.4 · Actors, reentrancy, isolated, nonisolated, global actors and @MainActor
- 10.5 · Region-based isolation, sending, and migrating to Swift 6
- 10.6 · Continuations and bridging GCD, Combine and callback APIs
- 10.7 · Deadlocks, main-actor hops, Thread Sanitizer and Instruments
- 10.8 · Task groups, async let, cancellation and task locals in practice
- 10.9 · Isolation rules, isolated parameters, global actors, main-actor hops and default isolation
- 10.10 · Sendable rules, locks versus actors, and the escape hatches
- 10.11 · Groups, semaphores, parallel loops, operation dependencies, QoS and timers