DevPrep

All topics › Combine

How do errors work in Combine: Failure, catch, retry, and why does the stream die?

Middle PRO

The gist

Every Publisher has a Failure type, and in Combine an error is not a value but a completion: .failure(error) is the last event of the stream; after it the publisher sends nothing and the subscription is released. That is why a single network error inside a chain "kills" it for good: the user taps "Refresh" and no more requests go out.

Error-handling operators do not "repair" the original stream — they replace it. catch switches to a fallback publisher, replaceError(with:) emits one value and finishes, retry(n) resubscribes to the upstream (for a cold publisher that means a new request).

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store