What is a Scheduler and the receive(on:)/subscribe(on:) operators?
The essence
Scheduler is a protocol describing where and when work runs: DispatchQueue, RunLoop, OperationQueue, ImmediateScheduler. It is needed by time operators (debounce, delay, throttle, timeout) and by the two context-switching operators.
receive(on:)— everything downstream (operators and the subscriber) receives values and completion on the given scheduler. This is how a result is delivered to the main thread before updating UI.subscribe(on:)— the subscription operations run on the given scheduler:subscribe,request(demand),cancel— i.e. the moment the upstream starts working. Where values are emitted is not guaranteed by the operator: the publisher itself decides.URLSession.dataTaskPublisherdelivers on its own queue,Subject.sendon the caller's thread, andsubscribe(on:)does not change that.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep