Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
105: AsyncSequence and AsyncStream Revisited
I remember a developer on my team a couple of years ago who spent nearly a week fighting a "callback hell" nightmare in a crypto price tracker we were building. He had a delegate method firing every 200ms with new price updates, and he was trying to sync that data with the UI using a mess of manual locks and `@Published` properties. The code was brittle, and he was seeing weird race conditions where the UI would flicker between old and new prices. When I showed him how to wrap that delegate in an AsyncStream, he actually looked relieved. We collapsed fifty lines of state management into a single for await loop. That is the real power of AsyncSequence: it lets you treat a continuous flow of events as if it were just a standard collection.
Bridging the Gap with Continuation
You’ve likely encountered AsyncSequence in the wild—think of URLSession.bytes(from:)—but the real magic happens when you need to create your own. Most of the time, you're dealing with legacy APIs or system frameworks that still rely on closures or delegates. AsyncStream is your bridge here.
The core of an AsyncStream is the continuation. This object is the handle you use to "yield" values into the stream from a non-async context. I always tell people to think of it as a pipe: the continuation is the mouth of the pipe where you pour data in, and the for await loop is where the data comes out.
struct PriceMonitor {
func streamPrices() -> AsyncStream<Double> {
AsyncStream { continuation in
// Imagine this is a legacy SDK callback
PriceSDK.shared.startUpdating { newPrice in
continuation.yield(newPrice)
}
// We handle the shutdown later
}
}
}
Notice how the AsyncStream closure gives us that continuation immediately. This allows us to set up our listener and start pushing values into the stream whenever the SDK decides they're ready.
Handling the Cleanup
One thing that often trips people up is the lifecycle of the stream. If you just yield values and forget about the producer, you've got a memory leak waiting to happen. If the task consuming the stream is cancelled, you need a way to tell the underlying SDK to stop sending updates.
The continuation.onTermination handler is where you do this. It's not optional if you're managing resources. I've seen too many apps crash because a background socket remained open long after the view controller that requested the data was dismissed.
AsyncStream { continuation in
let observer = PriceSDK.shared.observe { price in
continuation.yield(price)
}
continuation.onTermination = { termination in
switch termination {
case .cancelled:
print("Consumer stopped listening, killing the SDK observer.")
case .finished:
print("Stream completed naturally.")
}
observer.stop()
}
}
Processing the Stream in Real-time
Since AsyncStream conforms to AsyncSequence, you get all the functional tools you're used to, like filter and map. The crucial difference here is that these operations are lazy. They don't process the whole collection (because there is no "whole" collection); they process each element as it arrives from the continuation.
If you only care about price jumps greater than 5%, you don't need to put an if statement inside your loop. You can chain the logic directly onto the stream. It makes the calling code incredibly clean, almost like you're writing a declarative query for live data.
let priceStream = monitor.streamPrices()
// We filter and transform before we even start the loop
let significantChanges = priceStream
.filter { $0 > 100.0 }
.map { "Price alert: \($0)" }
for await alert in significantChanges {
print(alert)
}
Just keep in mind: once you start iterating with for await, the loop will suspend your task until the next value is yielded or the stream is closed. If you need to do other things while waiting for prices, you'll need to wrap that loop in its own Task.
📋 Practical Task
Exercise: Building a System Memory Pressure Monitor
Your goal is to create a utility that monitors "Memory Pressure" events. Since we don't have a real hardware sensor in this environment, you will simulate one using a Timer, but you must wrap it in an AsyncStream so the consumer can handle it using async/await.
Requirements:
- Create a class
MemoryPressureMonitorwith a methodpressureEvents() -> AsyncStream<MemoryLevel>. - Define an enum
MemoryLevelwith cases.normal,.warning, and.critical. - Inside the
AsyncStream, use aTimerto yield a randomMemoryLevelevery 1 second. - Crucial: Implement
continuation.onTerminationto invalidate the timer so it doesn't leak when the task is cancelled. - In your main code, create a
Taskthat iterates over this stream, but use afilterto only print a message when the level is.warningor.critical.
There are no comments for now.