What is Combine and its basic entities?
The essence
Combine is Apple's declarative framework (iOS 13) for working with streams of values over time: UI events, network responses, notifications, property changes. Instead of callbacks you describe a chain of transformations, and the framework manages subscription, delivery and completion.
The basic entities:
- Publisher — the source. Parameterized by
Output(the value type) andFailure(the error type,Neverif it cannot fail). Emits zero or more values, then exactly one completion:.finishedor.failure. - Subscriber — the consumer. Receives a
Subscription, requests demand, accepts values and completion. Built-in ones:sink,assign. - Subscription — the link between them; through it the subscriber requests values (
request(_:)) and cancels the stream (cancel()). Exposed outside asAnyCancellable. - Operator — a Publisher method returning a new Publisher (
map,filter,debounce,flatMap,combineLatest…). Operators are pure composition; they do nothing until subscribed. - Subject (
PassthroughSubject,CurrentValueSubject) — a Publisher you can feed imperatively; the bridge from non-reactive code. - Scheduler — where and when work runs (
DispatchQueue,RunLoop,OperationQueue).
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep