11.4 · receive(on:), subscribe(on:) and where code runs
Combine does not own threads. A value is delivered on whatever thread produced it, until you say otherwise with a scheduler.
Mental model. A pipeline has no thread of its own. The thread is wherever the value was made; receive(on:) moves everything downstream of it.
Threads and schedulers
Where each stage runs, and how to move it.
- What do receive(on:) and subscribe(on:) do, and how do you know which thread a stage runs on?
A scheduler (DispatchQueue, OperationQueue, RunLoop, ImmediateScheduler) decides when and where a closure runs. Combine does nothing to threads by default: a…
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.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