This is Understanding Combine, written by Matt Neuburg. It is a work in progress. Corrections and suggestions are greatly appreciated (you can comment here). So are donations; please consider keeping me going by funding this work at http://www.paypal.me/mattneub. Or buy my books: the current editions are iOS 13 Programming Fundamentals with Swift and Programming iOS 13. Thank you!


Joiners

Joiners
By joiners, I mean operators that join together the streams of values from multiple pipelines into a single stream. Each pipeline produces values; now we want those values to come together to form a single pipeline. The question is: How? What rule should be followed as we feed the values from multiple pipelines into one?

The joiner par excellence is .merge. Its rule is simple: if an upstream pipeline emits a value, pass it down this pipeline. We’ve already seen an example of a merge-like rule in action: that’s what .flatMap does if it produces more than one publisher. For example:

[1,2].publisher
    .flatMap { _ in
        Timer.publish(every: 1, on: .main, in: .common)
            .autoconnect()
            .scan(0) {i,_ in i+1}
    }

That pipeline produces

1
1
[one-second pause]
2
2
[one-second pause]
...

There are two timers, and they are both producing values simultaneously down the same pipeline. That’s a merge.


Table of Contents