11.8 · Future versus Deferred, subject replay, share, and bridging to async
Where a stream starts decides when work runs and who sees what. These questions are about eager versus lazy, what a late subscriber misses, and moving between Combine and async code.
Mental model. Ask of every publisher: does it run before anyone subscribes, does a second subscriber repeat the work, and what does a late subscriber get?
Eager, lazy and replay
Future versus Deferred, how subjects replay and complete, and what share does for late subscribers.
- When does a Future run its work, what do two subscribers get, and what does Deferred change?
A Future is eager: its closure runs when you create it, before anyone subscribes, which is why the log shows “2 Future closure runs” before “3 Future created… - What happens when you send to a subject after it has completed, and what does a late subscriber to a finished subject receive?
A PassthroughSubject has no memory: a subscriber receives only the values sent after it subscribed. The 0 sent before subscribing is lost (the log starts at… - What does share() do for several subscribers, and what happens to a subscriber who arrives after the shared work has finished?
share() makes the upstream publisher run once for all current subscribers: it subscribes upstream when the first subscriber arrives and fans the values out. In…
Bridging
Publisher values as an async sequence, and Future around async work.
- How do you move between Combine and async/await: the values property, Future around a Task, and what to watch for?
Combine to async: every publisher with Failure == Never has a .values property that is an AsyncSequence (an AsyncPublisher; AsyncThrowingPublisher for failing…
More in Combine
- 11.1 · Publisher, Subscriber, Subscription and demand
- 11.2 · map, scan, combineLatest, flatMap, switchToLatest, debounce, share
- 11.3 · PassthroughSubject, CurrentValueSubject and @Published
- 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.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