特别是,异步流是 well-suited 以适应基于回调或委托的 API 以参与async-await。 与AsyncStream相比,此类型可以从等待的next()引发错误,这会终止带有引发错误的流。 您使用接收AsyncThrowingStream.Continuation的闭包初始化AsyncThrowingStream。在此闭包中生成元素,然后通过调用延续的yield(_:)方法将它们提供给流。当没...
为了使该阅读器兼容 Swift Concurrency,我进行了扩展并创建了AsyncStream似乎最适合这种情况的扩展。 publicextension StreamedFileReader {funcread() -> AsyncStream<DataChunk>{ AsyncStream { continuationinself.read(nextChunk: { chunkincontinuation.yield(chunk) }, completion: { continuation.finish() }) } ...
In an earlier post, I wrote about different ways that you canbridge your existing asynchronous codeover to Swift’s new Concurrency system that leverages async / await. The mechanisms shown there work great for code where your code produces a single result that can be modeled as a single value...
Swift AsyncStream AsyncStream.Iterator Structure AsyncStream.Iterator The asynchronous iterator for iterating an asynchronous stream. iOS 13.0+iPadOS 13.0+Mac Catalyst 13.0+macOS 10.15+tvOS 13.0+visionOS 1.0+watchOS 6.0+ struct Iterator Overview This type doesn’t conform to Sendable. Don’t use it...
()method. This causes the sequence iterator to produce anil, which terminates the sequence. If an error occurs, call the continuation’sfinish(throwing:)method, which causes the iterator’snext()method to throw the error to the awaiting call point. The continuation isSendable, which permits ...
Swift Swift Standard Library Concurrency AsyncStream AsyncStream.Iterator Structure The asynchronous iterator for iterating an asynchronous stream. iOS 13.0+iPadOS 13.0+Mac Catalyst 13.0+macOS 10.15+tvOS 13.0+visionOS 1.0+watchOS 6.0+ structIterator ...
()method. This causes the sequence iterator to produce anil, which terminates the sequence. If an error occurs, call the continuation’sfinish(throwing:)method, which causes the iterator’snext()method to throw the error to the awaiting call point. The continuation isSendable, which permits ...
()method. This causes the sequence iterator to produce anil, which terminates the sequence. If an error occurs, call the continuation’sfinish(throwing:)method, which causes the iterator’snext()method to throw the error to the awaiting call point. The continuation isSendable, which permits ...