DevPrep

All topics › Combine

Cold and hot publishers: share, multicast and Future — what runs how many times?

Senior PRO

The gist

A cold publisher starts its work anew on every subscription and separately for each subscriber: dataTaskPublisher, Just, Deferred, Sequence.publisher. Two sinks on one dataTaskPublisher mean two HTTP requests. A hot publisher produces values independently of subscribers and hands the same ones to all current subscribers: subjects, @Published, NotificationCenter.publisher, Timer.publish after connect.

Operators inherit coldness: map, decode, handleEvents run separately for each subscriber. Hence duplicated requests, double logging and side effects executed N times.

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