In Combine Framework — Difference Between Merge, Zip, and CombineLatest

Manish Pathak
3 min readMay 27, 2024

--

Combine is a framework in Swift for processing values over time. Today I want to talk about running multiple operations in parallel and handing results in a single place. When you want to combine the output from multiple publishers, you can use various operators provided by Combine. Here’s a brief overview of how you can combine multiple publishers in Swift using Combine:

Merge

The merge operator combines the elements from multiple publishers into a single publisher. You can use mergeto merge different publishers with the same output type.

import Combine
let publisher1 = PassthroughSubject<Int, Never>()
let publisher2 = PassthroughSubject<Int, Never>()
let merged = Publishers.Merge(publisher1, publisher2)let cancellable = merged.sink { value in
print(value)
}
publisher1.send(1)
publisher2.send(2)
// Output: 1, 2

CombineLatest

The combineLatest operator does the same thing as merge but can merge different publishers with different output types.

import Combine
let publisher1 = PassthroughSubject<Int, Never>()
let publisher2 = PassthroughSubject<String, Never>()
let combined = Publishers.CombineLatest(publisher1, publisher2)let cancellable = combined.sink { value in
print(value)
}
publisher1.send(1)
publisher2.send("A")
// Output: (1, "A")

Zip:

The zip operator combines elements from multiple publishers into tuples. You can use zip operator when you have a couple of publishers and need to wait for values from all of them. The main downside of the zip operator is that it delivers values only when all the publishers emit.

import Combine
let publisher1 = PassthroughSubject<Int, Never>()
let publisher2 = PassthroughSubject<String, Never>()
let zipped = Publishers.Zip(publisher1, publisher2)let cancellable = zipped.sink { value in
print(value)
}
publisher1.send(1)
publisher2.send("A")
// Output: (1, "A")publisher1.send(2)
// not going to work

you can see from the code above, that zipped publishers through zip() the operator will not be called unless provided a pair. In combineLatest()operator, the difference is — when its first published value went down to the downstream, you can even receive the event from one publisher. But zip() is different because it always needs a pair of emitted events to form a tuple value.

Merge vs. CombineLatest vs. Zip: Which One to Choose to Use in iOS?

Now that we’ve covered the basics of merge, combineLatest, and zip, you might be wondering when to use each of these operators in your projects. Here’s a quick cheat sheet to help you decide:

  • Use merge when you want to combine the output of two publishers into a single stream, emitting values as they arrive from either publisher without any synchronization.
  • Use combineLatest when you need to perform an action based on the latest values from multiple publishers, emitting a tuple containing the latest values from each publisher every time one of them produces a new value.
  • Use zip when you need to synchronize values from different publishers based on their order, emitting a tuple of values when all publishers have emitted a value.

These are just a few examples of combining publishers in Combine. Depending on your use case, you may choose the appropriate operator that best fits your requirements. Additionally, Combine provides many other operators for transforming, filtering, and manipulating values from publishers.

--

--