Hello, I was hoping to clarify my understanding of the use of for await with an AsyncStream. My use case is, I'd like to yield async closures to the stream's continuation, with the idea that, when I use for await with the stream to process and execute the closures, it would only continue on to the following closure once the current closure has been run to completion.
At a high level, I am trying to implement in-order execution of async closures in the context of re-entrancy. An example of asynchronous work I want to execute is a network call that should write to a database:
func syncWithRemote() async -> Void {
let data = await fetchDataFromNetwork()
await writeToLocalDatabase(data)
}
For the sake of example, I'll call the intended manager of closure submission SingleOperationRunner.
where, at a use site such as this, my desired outcome is that call 1 of syncWithRemote() is always completed before call 2 of it:
let singleOperationRunner = SingleOperationRunner(priority: nil)
singleOperationRunner.run {
syncWithRemote()
}
singleOperationRunner.run {
syncWithRemote()
}
My sketch implementation looks like this:
public final class SingleOperationRunner {
private let continuation: AsyncStream<() async -> Void>.Continuation
public init(priority: TaskPriority?) {
let (stream, continuation) = AsyncStream.makeStream(of: (() async -> Void).self)
self.continuation = continuation
Task.detached(priority: priority) {
// Will this loop only continue when the `await operation()` completes?
for await operation in stream {
await operation()
}
}
}
public func run(operation: @escaping () async -> Void) {
continuation.yield(operation)
}
deinit {
continuation.finish()
}
}
The resources I've found are https://developer.apple.com/videos/play/wwdc2022-110351/?time=1445 and https://forums.swift.org/t/swift-async-func-to-run-sequentially/60939/2 but do not think I have fully put the pieces together, so would appreciate any help!
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hello! I am trying to determine the best approach with AVPlayer for implementing auto-play, that is, playback that automatically starts without user initiation. Ideally this would work for both local and streaming audio.
My current approach is using KVO and the status on an AVPlayerItem equal to readyToPlay to do this, but I was wondering if there was a better property or state to use, or, alternatively, whether this use case may already be handled when automaticallyWaitsToMinimizeStalling is true, so that I could simply write:
player.replaceCurrentItem(with: AVPlayerItem(url: streamingUrl))
player.rate = 1
or
let playerItem = AVPlayerItem(url: streamingUrl)
player = AVPlayer(playerItem: playerItem)
player.rate = 1
and expect the item to be auto-played when ready.
In the context of user-initiated playback, I've typically seen code that makes a button's enabled state contingent on player.currentItem.duration, e.g. in AVFoundationSimplePlayer-iOS. On the other hand, AVAutoWait, which utilizes automaticallyWaitsToMinimizeStalling, does not seem to do this.
As a side note, I am not using an AVQueuePlayer.
Topic:
Media Technologies
SubTopic:
Streaming