enum AsyncStream.Continuation.BufferingPolicy
struct AsyncStream.Continuation
Availability
Technology
init(_ elementType: Element.Type = Element.self, bufferingPolicy limit: AsyncStream
<Element>.Continuation
.BufferingPolicy
= .unbounded, _ build: (AsyncStream
<Element>.Continuation
) -> Void
)
elementType
The type of element the Async
produces.
bufferingPolicy
A Continuation
value to set the stream’s buffering behavior. By default, the stream buffers an unlimited number of elements. You can also set the policy to buffer a specified number of oldest or newest elements.
build
A custom closure that yields values to the Async
. This closure receives an Async
instance that it uses to provide elements to the stream and terminate the stream when finished.
The Async
received by the build
closure is appropriate for use in concurrent contexts. It is thread safe to send and finish; all calls to the continuation are serialized. However, calling this from multiple concurrent contexts could result in out-of-order delivery.
The following example shows an Async
created with this initializer that produces 100 random numbers on a one-second interval, calling yield(_:)
to deliver each element to the awaiting call point. When the for
loop exits and the stream finishes by calling the continuation’s finish()
method.
enum AsyncStream.Continuation.BufferingPolicy
struct AsyncStream.Continuation