11.3 · PassthroughSubject, CurrentValueSubject and @Published
Subjects are publishers you can push values into by hand: the bridge from imperative code into a pipeline. @Published is the same idea built into a property.
Mental model. A subject is a mailbox you can post to. Passthrough is a mailbox with no memory; current-value is a noticeboard that always shows the latest notice.
Pushing values in
The two subjects and the property wrapper that wraps one.
- What is the difference between PassthroughSubject and CurrentValueSubject, and when do you use a subject at all?
A subject is both a publisher and something you can send values and completions into. PassthroughSubject<Output, Failure> has no state: it forwards values to… - How does @Published work, and why does a subscriber see the old value inside its sink?
@Published is a property wrapper that stores the value and exposes a publisher through the projected value: $name is a Published<T>.Publisher (never fails).…
More in Combine
- 11.1 · Publisher, Subscriber, Subscription and demand
- 11.2 · map, scan, combineLatest, flatMap, switchToLatest, debounce, share
- 11.4 · receive(on:), subscribe(on:) and where code runs
- 11.5 · AnyCancellable, subscription lifetime, and failure versus Never
- 11.6 · Combine versus async/await, and testing pipelines
- 11.7 · Operator behaviour, error placement, retry, flatMap limits and debugging
- 11.8 · Future versus Deferred, subject replay, share, and bridging to async
- 11.9 · Which thread each stage runs on, schedulers, throttle versus debounce, and timers
- 11.10 · Cancelling and completing, the cycle that survives a deinit, and failure types