Generic Class

CurrentValueSubject

A subject that wraps a single value and publishes a new element whenever the value changes.

Declaration

final class CurrentValueSubject<Output, Failure> where Failure : Error

Overview

Unlike PassthroughSubject, CurrentValueSubject maintains a buffer of the most recently published element.

Calling send(_:) on a CurrentValueSubject also updates the current value, making it equivalent to updating the value directly.

Topics

Creating a Current Value Subject

init(Output)

Creates a current value subject with the given initial value.

Accessing the Current Value

var value: Output

The value wrapped by this subject, published as a new element whenever it changes.

Instance Methods

func allSatisfy((Output) -> Bool) -> Publishers.AllSatisfy<CurrentValueSubject<Output, Failure>>

Publishes a single Boolean value that indicates whether all received elements pass a given predicate.

func assertNoFailure(String, file: StaticString, line: UInt) -> Publishers.AssertNoFailure<CurrentValueSubject<Output, Failure>>

Raises a fatal error when its upstream publisher fails, and otherwise republishes all received input.

func assign<Root>(to: ReferenceWritableKeyPath<Root, Output>, on: Root) -> AnyCancellable

Assigns a publisher’s output to a property of an object.

func `catch`<P>((Failure) -> P) -> Publishers.Catch<CurrentValueSubject<Output, Failure>, P>

Handles errors from an upstream publisher by replacing it with another publisher.

func collect() -> Publishers.Collect<CurrentValueSubject<Output, Failure>>

Collects all received elements, and emits a single array of the collection when the upstream publisher finishes.

func collect(Int) -> Publishers.CollectByCount<CurrentValueSubject<Output, Failure>>

Collects up to the specified number of elements, and then emits a single array of the collection.

func combineLatest<P>(P) -> Publishers.CombineLatest<CurrentValueSubject<Output, Failure>, P>

Subscribes to an additional publisher and publishes a tuple upon receiving output from either publisher.

func combineLatest<P, Q>(P, Q) -> Publishers.CombineLatest3<CurrentValueSubject<Output, Failure>, P, Q>

Subscribes to two additional publishers and publishes a tuple upon receiving output from any of the publishers.

func combineLatest<P, T>(P, (Output, P.Output) -> T) -> Publishers.Map<Publishers.CombineLatest<CurrentValueSubject<Output, Failure>, P>, T>

Subscribes to an additional publisher and invokes a closure upon receiving output from either publisher.

func combineLatest<P, Q, R>(P, Q, R) -> Publishers.CombineLatest4<CurrentValueSubject<Output, Failure>, P, Q, R>

Subscribes to three additional publishers and publishes a tuple upon receiving output from any of the publishers.

func combineLatest<P, Q, T>(P, Q, (Output, P.Output, Q.Output) -> T) -> Publishers.Map<Publishers.CombineLatest3<CurrentValueSubject<Output, Failure>, P, Q>, T>

Subscribes to two additional publishers and invokes a closure upon receiving output from any of the publishers.

func compactMap<T>((Output) -> T?) -> Publishers.CompactMap<CurrentValueSubject<Output, Failure>, T>

Calls a closure with each received element and publishes any returned optional that has a value.

func contains(Output) -> Publishers.Contains<CurrentValueSubject<Output, Failure>>

Publishes a Boolean value upon receiving an element equal to the argument.

func contains(where: (Output) -> Bool) -> Publishers.ContainsWhere<CurrentValueSubject<Output, Failure>>

Publishes a Boolean value upon receiving an element that satisfies the predicate closure.

func count() -> Publishers.Count<CurrentValueSubject<Output, Failure>>

Publishes the number of elements received from the upstream publisher.

func drop<P>(untilOutputFrom: P) -> Publishers.DropUntilOutput<CurrentValueSubject<Output, Failure>, P>

Ignores elements from the upstream publisher until it receives an element from a second publisher.

func drop(while: (Output) -> Bool) -> Publishers.DropWhile<CurrentValueSubject<Output, Failure>>

Omits elements from the upstream publisher until a given closure returns false, before republishing all remaining elements.

func dropFirst(Int) -> Publishers.Drop<CurrentValueSubject<Output, Failure>>

Omits the specified number of elements before republishing subsequent elements.

func encode<Coder>(encoder: Coder) -> Publishers.Encode<CurrentValueSubject<Output, Failure>, Coder>

Encodes the output from upstream using a specified encoder.

func eraseToAnyPublisher() -> AnyPublisher<Output, Failure>

Wraps this publisher with a type eraser.

func first() -> Publishers.First<CurrentValueSubject<Output, Failure>>

Publishes the first element of a stream, then finishes.

func first(where: (Output) -> Bool) -> Publishers.FirstWhere<CurrentValueSubject<Output, Failure>>

Publishes the first element of a stream to satisfy a predicate closure, then finishes.

func ignoreOutput() -> Publishers.IgnoreOutput<CurrentValueSubject<Output, Failure>>

Ingores all upstream elements, but passes along a completion state (finished or failed).

func last() -> Publishers.Last<CurrentValueSubject<Output, Failure>>

Publishes only the last element of a stream, after the stream finishes.

func last(where: (Output) -> Bool) -> Publishers.LastWhere<CurrentValueSubject<Output, Failure>>

Publishes only the last element of a stream that satisfies a predicate closure, after the stream finishes.

func makeConnectable() -> Publishers.MakeConnectable<CurrentValueSubject<Output, Failure>>

Creates a connectable wrapper around the publisher.

func map<T>((Output) -> T) -> Publishers.Map<CurrentValueSubject<Output, Failure>, T>

Transforms all elements from the upstream publisher with a provided closure.

func mapError<E>((Failure) -> E) -> Publishers.MapError<CurrentValueSubject<Output, Failure>, E>

Converts any failure from the upstream publisher into a new error.

func max() -> Publishers.Comparison<CurrentValueSubject<Output, Failure>>

Publishes the maximum value received from the upstream publisher, after it finishes.

func max(by: (Output, Output) -> Bool) -> Publishers.Comparison<CurrentValueSubject<Output, Failure>>

Publishes the maximum value received from the upstream publisher, using the provided ordering closure.

func merge(with: CurrentValueSubject<Output, Failure>) -> Publishers.MergeMany<CurrentValueSubject<Output, Failure>>

Combines elements from this publisher with those from another publisher of the same type, delivering an interleaved sequence of elements.

func merge<B, C>(with: B, C) -> Publishers.Merge3<CurrentValueSubject<Output, Failure>, B, C>

Combines elements from this publisher with those from two other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D>(with: B, C, D) -> Publishers.Merge4<CurrentValueSubject<Output, Failure>, B, C, D>

Combines elements from this publisher with those from three other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D, E>(with: B, C, D, E) -> Publishers.Merge5<CurrentValueSubject<Output, Failure>, B, C, D, E>

Combines elements from this publisher with those from four other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D, E, F>(with: B, C, D, E, F) -> Publishers.Merge6<CurrentValueSubject<Output, Failure>, B, C, D, E, F>

Combines elements from this publisher with those from five other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D, E, F, G>(with: B, C, D, E, F, G) -> Publishers.Merge7<CurrentValueSubject<Output, Failure>, B, C, D, E, F, G>

Combines elements from this publisher with those from six other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D, E, F, G, H>(with: B, C, D, E, F, G, H) -> Publishers.Merge8<CurrentValueSubject<Output, Failure>, B, C, D, E, F, G, H>

Combines elements from this publisher with those from seven other publishers, delivering an interleaved sequence of elements.

func min() -> Publishers.Comparison<CurrentValueSubject<Output, Failure>>

Publishes the minimum value received from the upstream publisher, after it finishes.

func min(by: (Output, Output) -> Bool) -> Publishers.Comparison<CurrentValueSubject<Output, Failure>>

Publishes the minimum value received from the upstream publisher, after it finishes.

func multicast<S>(() -> S) -> Publishers.Multicast<CurrentValueSubject<Output, Failure>, S>

Applies a closure to create a subject that delivers elements to subscribers.

func multicast<S>(subject: S) -> Publishers.Multicast<CurrentValueSubject<Output, Failure>, S>

Provides a subject to deliver elements to multiple subscribers.

func output(at: Int) -> Publishers.Output<CurrentValueSubject<Output, Failure>>

Publishes a specific element, indicated by its index in the sequence of published elements.

func output<R>(in: R) -> Publishers.Output<CurrentValueSubject<Output, Failure>>

Publishes elements specified by their range in the sequence of published elements.

func prefix(Int) -> Publishers.Output<CurrentValueSubject<Output, Failure>>

Republishes elements up to the specified maximum count.

func prefix(while: (Output) -> Bool) -> Publishers.PrefixWhile<CurrentValueSubject<Output, Failure>>

Republishes elements while a predicate closure indicates publishing should continue.

func reduce<T>(T, (T, Output) -> T) -> Publishers.Reduce<CurrentValueSubject<Output, Failure>, T>

Applies a closure that accumulates each element of a stream and publishes a final result upon completion.

func removeDuplicates() -> Publishers.RemoveDuplicates<CurrentValueSubject<Output, Failure>>

Publishes only elements that don’t match the previous element.

func removeDuplicates(by: (Output, Output) -> Bool) -> Publishers.RemoveDuplicates<CurrentValueSubject<Output, Failure>>

Publishes only elements that don’t match the previous element, as evaluated by a provided closure.

func replaceNil<T>(with: T) -> Publishers.Map<CurrentValueSubject<Output, Failure>, T>

Replaces nil elements in the stream with the provided element.

func retry(Int) -> Publishers.Retry<CurrentValueSubject<Output, Failure>>

Attempts to recreate a failed subscription with the upstream publisher using a specified number of attempts to establish the connection.

func scan<T>(T, (T, Output) -> T) -> Publishers.Scan<CurrentValueSubject<Output, Failure>, T>

Transforms elements from the upstream publisher by providing the current element to a closure along with the last value returned by the closure.

func send()

Sends a void value to the subscriber.

func setFailureType<E>(to: E.Type) -> Publishers.SetFailureType<CurrentValueSubject<Output, Failure>, E>

Changes the failure type declared by the upstream publisher.

func sink(receiveValue: ((Output) -> Void)) -> AnyCancellable

Attaches a subscriber with closure-based behavior to a publisher that never fails.

func subscribe<S>(S) -> AnyCancellable

Attaches the specified subject to this publisher.

func subscribe<S>(S)

Attaches the specified subscriber to this publisher.

func subscribe<S>(on: S, options: S.SchedulerOptions?) -> Publishers.SubscribeOn<CurrentValueSubject<Output, Failure>, S>

Specifies the scheduler on which to perform subscribe, cancel, and request operations.

func switchToLatest() -> Publishers.SwitchToLatest<Output, CurrentValueSubject<Output, Failure>>

Flattens the stream of events from multiple upstream publishers to appear as if they were coming from a single stream of events.

func throttle<S>(for: S.SchedulerTimeType.Stride, scheduler: S, latest: Bool) -> Publishers.Throttle<CurrentValueSubject<Output, Failure>, S>

Publishes either the most-recent or first element published by the upstream publisher in the specified time interval.

func tryAllSatisfy((Output) -> Bool) -> Publishers.TryAllSatisfy<CurrentValueSubject<Output, Failure>>

Publishes a single Boolean value that indicates whether all received elements pass a given error-throwing predicate.

func tryCatch<P>((Failure) -> P) -> Publishers.TryCatch<CurrentValueSubject<Output, Failure>, P>

Handles errors from an upstream publisher by replacing it with another publisher or an error.

func tryCompactMap<T>((Output) -> T?) -> Publishers.TryCompactMap<CurrentValueSubject<Output, Failure>, T>

Calls an error-throwing closure with each received element and publishes any returned optional that has a value.

func tryContains(where: (Output) -> Bool) -> Publishers.TryContainsWhere<CurrentValueSubject<Output, Failure>>

Publishes a Boolean value upon receiving an element that satisfies the throwing predicate closure.

func tryDrop(while: (Output) -> Bool) -> Publishers.TryDropWhile<CurrentValueSubject<Output, Failure>>

Omits elements from the upstream publisher until an error-throwing closure returns false, before republishing all remaining elements.

func tryFilter((Output) -> Bool) -> Publishers.TryFilter<CurrentValueSubject<Output, Failure>>

Republishes all elements that match a provided error-throwing closure.

func tryFirst(where: (Output) -> Bool) -> Publishers.TryFirstWhere<CurrentValueSubject<Output, Failure>>

Publishes the first element of a stream to satisfy a throwing predicate closure, then finishes.

func tryLast(where: (Output) -> Bool) -> Publishers.TryLastWhere<CurrentValueSubject<Output, Failure>>

Publishes only the last element of a stream that satisfies an error-throwing predicate closure, after the stream finishes.

func tryMap<T>((Output) -> T) -> Publishers.TryMap<CurrentValueSubject<Output, Failure>, T>

Transforms all elements from the upstream publisher with a provided error-throwing closure.

func tryMax(by: (Output, Output) -> Bool) -> Publishers.TryComparison<CurrentValueSubject<Output, Failure>>

Publishes the maximum value received from the upstream publisher, using the provided error-throwing closure to order the items.

func tryMin(by: (Output, Output) -> Bool) -> Publishers.TryComparison<CurrentValueSubject<Output, Failure>>

Publishes the minimum value received from the upstream publisher, using the provided error-throwing closure to order the items.

func tryPrefix(while: (Output) -> Bool) -> Publishers.TryPrefixWhile<CurrentValueSubject<Output, Failure>>

Republishes elements while a error-throwing predicate closure indicates publishing should continue.

func tryReduce<T>(T, (T, Output) -> T) -> Publishers.TryReduce<CurrentValueSubject<Output, Failure>, T>

Applies an error-throwing closure that accumulates each element of a stream and publishes a final result upon completion.

func tryRemoveDuplicates(by: (Output, Output) -> Bool) -> Publishers.TryRemoveDuplicates<CurrentValueSubject<Output, Failure>>

Publishes only elements that don’t match the previous element, as evaluated by a provided error-throwing closure.

func tryScan<T>(T, (T, Output) -> T) -> Publishers.TryScan<CurrentValueSubject<Output, Failure>, T>

Transforms elements from the upstream publisher by providing the current element to an error-throwing closure along with the last value returned by the closure.

func zip<P>(P) -> Publishers.Zip<CurrentValueSubject<Output, Failure>, P>

Combines elements from another publisher and deliver pairs of elements as tuples.

func zip<P, Q>(P, Q) -> Publishers.Zip3<CurrentValueSubject<Output, Failure>, P, Q>

Combines elements from two other publishers and delivers groups of elements as tuples.

func zip<P, Q, R>(P, Q, R) -> Publishers.Zip4<CurrentValueSubject<Output, Failure>, P, Q, R>

Combines elements from three other publishers and delivers groups of elements as tuples.

Relationships

Conforms To

See Also

Subjects

protocol Subject

A publisher that exposes a method for outside callers to publish elements.

class PassthroughSubject

A subject that broadcasts elements to downstream subscribers.