combineLatest allows you to combine multiple publishers together into a single stream, and it will only start emitting once all publishers have emitted at least one value. After that initial condition is met, any emission from any of the publishers will trigger the sink with the latest values from all of them. Combine natively supports up to 4 publishers in a single combineLatest call, meaning you can pass up to 3 additional publishers alongside the one you're chaining from. If you need more than 4 you have to nest multiple combineLatest calls together, though this gets messy quickly and in that scenario you'd be better off restructuring your code using @Published properties in a ViewModel with a single computed publisher that handles all the logic cleanly.

Concept

In Combine, multiple publishers can be combined into a single stream.

This allows values coming from different sources to be processed together.

A common operator used for this purpose is:

combineLatest

combineLatest merges two publishers and emits a value whenever either publisher produces a new value, using the latest values from both streams.

Conceptually:

Publisher A
            \
             combineLatest → Subscriber
            /
Publisher B

Example Scenario

In the example view controller, two text fields are used:

Username TextField
Password TextField

Each text field produces a stream of text values.

These values are pushed into two subjects:

let usernameSubject = PassthroughSubject<String, Never>()
let passwordSubject = PassthroughSubject<String, Never>()

Pipeline

The Combine pipeline is created using combineLatest.