DevPrep

All topics › Combine

What is backpressure/demand in Combine?

Senior PRO

The essence

Backpressure is the mechanism by which a slow consumer holds back a fast producer. In Combine it is implemented through demand: the subscriber explicitly tells the publisher how many values it is ready to accept (Subscribers.Demand — .none, .max(n), .unlimited), and by contract the publisher may not send more. Demand accumulates: every request(.max(n)) adds to the current amount, and the value returned from receive(_:) buys more. It is a pull model on top of push delivery.

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