Browse Questions
- All three are test doubles — fake objects used in place of real dependencies — but with different roles.
- AsyncStream converts callback or delegate-based event sources into an AsyncSequence you can iterate over with for await.
- Generate the pair with TSKeyGen, then persist only the private key in the Keychain with a restrictive accessibility class and hand the public key to the server during enrolment.
- Both static and class define type-level properties and methods, but static members cannot be overridden by subclasses (final), whereas class members allow dynamic dispatch and can be overridden.
Answer:
AsyncStream converts callback or delegate-based event sources into an AsyncSequence you can iterate over with for await. It bridges event-driven code into structured concurrency.
Code Example:
// Wrapping a delegate/callback pattern into AsyncStream
func locationUpdates() -> AsyncStream<CLLocation> {
AsyncStream { continuation in
let manager = CLLocationManager()
let delegate = LocationDelegate { location in
continuation.yield(location) // emit each update
}
manager.delegate = delegate
manager.startUpdatingLocation()
continuation.onTermination = { _ in
manager.stopUpdatingLocation() // cleanup on cancel
}
}
}
// Consuming it cleanly with for await
Task {
for await location in locationUpdates() {
print("New location: \(location.coordinate)")
}
}
Use for: Location updates, WebSocket messages, notifications, sensor data, any ongoing event stream.