SharedAsyncSequence

public struct SharedAsyncSequence<Base> : AsyncSequence where Base : AsyncSequence

An async sequence that can be shared between multiple tasks.

let values = [
    "a",
    "ab",
    "abc",
    "abcd"
]

let stream = AsyncStream { continuation in
    for value in values {
        continuation.yield(value)
    }
    continuation.finish()
}
.shared()

Task {
    let values = try await self.stream.collect()
    // ...
}

Task.detached {
    let values = try await self.stream.collect()
    // ...
}

let values = try await self.stream.collect()
// ...
  • Declaration

    Swift

    public typealias AsyncIterator = AsyncThrowingStream<Base.Element, Error>.Iterator
  • The kind of elements streamed.

    Declaration

    Swift

    public typealias Element = Base.Element

SharedAsyncSequence (Public Properties)

  • Creates a shareable async sequence that can be used across multiple tasks.

    Declaration

    Swift

    public init(_ base: Base)

    Parameters

    base

    The async sequence in which this sequence receives it’s elements.

AsyncSequence

  • Creates an async iterator that emits elements of this async sequence.

    Declaration

    Swift

    public func makeAsyncIterator() -> AsyncThrowingStream<Base.Element, Error>.Iterator

    Return Value

    An instance that conforms to AsyncIteratorProtocol.