11.5 · AnyCancellable, subscription lifetime, and failure versus Never
Two things end a pipeline early: you cancel it, or an error terminates it. Both are easy to get wrong silently, so you need to know exactly what each does.
Mental model. A subscription lives exactly as long as its cancellable, and an error is terminal. Keep the first alive, and handle the second inside any pipeline that must keep running.
Lifetime
What keeps a subscription alive and what cancels it.
- What is AnyCancellable, how long does a subscription live, and how do you manage it in a view model?
sink and assign return an AnyCancellable. It is the handle that keeps the subscription alive: while it exists the subscription is active, and when it is…
Failure
Errors, <code>Never</code> and recovery operators.
- How do failures work in Combine, what does Never mean, and how do catch, replaceError, retry and mapError differ?
Every publisher has a Failure type. Never means “cannot fail”, and it unlocks assign and the short sink { } without a completion handler. An error is terminal…
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.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