Hallo honden en katten, Leo hier! Today let’s understand the main differences between merge, CombineLatest, and Zip operators in Combine for iOS.

If you’re anything like me, you’re always on the hunt for the most efficient and elegant way to deal with asynchronous events and data streams. And if that’s the case, you’ve probably come across Apple’s Combine framework – the new(ish) kid on the block that’s making waves in the world of reactive programming for iOS.

Combine offers a rich array of operators to help you transform, filter, and combine the output of publishers. But with great power comes great confusion, am I right? Today, we’ll dive into three of Combine’s most popular operators: merge, combineLatest, and zip.

You might be wondering how to use reactive programming before Combine. You can dive into that world with this article about boxing techniques for Swift! Learn how to encapsulate state changes and manage complex async code with ease.

Some time ago I wrote an article about how the Combine framework handled threads. Turns out that is one of my most popular articles for the blog.

 

So buckle up and let’s get started! But first…

 

Painting of the Day

I chose the 1794 painting called Pinkie by Master Thomas Lawrence. 

This painting is displayed at the Huntington Library in San Marino, California. It’s usually displayed right across from “The Blue Boy” by Thomas Gainsborough. The museum calls it “Sarah Goodin Barrett Moulton: ‘Pinkie'”, and these two paintings are like the stars of their art collection, which has a bunch of cool 18th-century British portraits.

The painting shows Sarah Moulton, who was around 11 years old, and the way she’s looking straight at you with all those lively brushstrokes makes it feel super fresh and real. 

The lively brushstrokes are what made me add this painting here. You can spot in the sky behind the girl, that the brushstrokes kinda merge with each other, and that is what we will talk about today, dealing with different streams of colors ( values ).

 

The Problem – Dealing with Multiple Streams in Combine for iOS

You have several publishers that you want to work with using Combine.

Combine provides several operators to combine the output of multiple publishers, allowing you to merge or synchronize values from different data streams.

Here’s a brief overview of three common operators used for combining publishers:

  1. merge: This operator combines two publishers of the same type into one, emitting values as they arrive from either publisher. The resulting publisher emits values from both original publishers as they become available, interweaving the values in the order they’re received.
  2. combineLatest: This operator combines the latest values from multiple publishers whenever any of them emit a new value. The resulting publisher emits a tuple containing the latest values from each publisher every time one of them produces a new value. This is useful when you need to perform an action based on the latest values from different sources.
  3. zip: This operator combines values from multiple publishers pair-wise, emitting a tuple of values when all publishers have emitted a value. The resulting publisher waits for each publisher to emit a value, then combines the values into a tuple and sends it to the subscriber. This process repeats for subsequent values. This is useful when you need to synchronize values from different publishers based on their orders.

Now let’s deep dive into each one of them with code samples!

 

Merge: Bringing Publishers Together in Perfect Harmony

Picture this: you’ve got two publishers, both emitting values over time, and you want to combine their output into a single stream. How do you make it happen? Enter merge.

The merge the operator is the superhero that swoops in to combine two publishers of the same type into one, emitting values as they arrive from either publisher. The merged publisher doesn’t discriminate between values from the original publishers – it simply emits them in the order they’re received.

Here’s a quick example to get you started, remember that this is a minimum example, you probably want to save your sink into a cancelable:

let publisher1 = PassthroughSubject<Int, Never>()
let publisher2 = PassthroughSubject<Int, Never>()

let mergedPublisher = publisher1.merge(with: publisher2)
    .sink { value in
        print("Received value: \(value)")
    }

publisher1.send(1) // Output: Received value: 1
publisher2.send(2) // Output: Received value: 2
publisher1.send(3) // Output: Received value: 3

As you can see, mergedPublisher receives and emits values from both publisher1 and publisher2 as they arrive, combining them into a single stream. It’s that easy!

 

CombineLatest: The Operator for When Timing is Everything

Next up, let’s talk about combineLatest.

This operator is all about timing. It combines the latest values from multiple publishers whenever any of them emit a new value. So, every time one publisher updates, the resulting publisher emits a tuple containing the latest values from each publisher.

This is super useful when you need to perform an action based on the latest values from different sources.

Check out this example:

let temperaturePublisher = PassthroughSubject<Int, Never>()
let humidityPublisher = PassthroughSubject<Int, Never>()

let weatherPublisher = Publishers.CombineLatest(temperaturePublisher, humidityPublisher)
    .sink { temperature, humidity in
        print("Temperature: \(temperature), Humidity: \(humidity)")
    }

temperaturePublisher.send(72) // No output yet
humidityPublisher.send(45)     // Output: Temperature: 72, Humidity: 45
temperaturePublisher.send(74) // Output: Temperature: 74, Humidity: 45

In this case, the weatherPublisher combines the latest values from temperaturePublisher and humidityPublisher and emits a tuple every time one of them produces a new value. It’s like magic, but better!

 

Zip: The Operator That Keeps Things in Sync

Last but not least, we have the zip operator. This operator is all about synchronization. It combines values from multiple publishers pair-wise, emitting a tuple of values when all publishers have emitted a value.

The resulting publisher waits for each publisher to emit a value, then combines the values into a tuple and sends it to the subscriber. This process repeats for subsequent values. Zip is perfect when you need to synchronize values from different publishers based on their order.

Here’s a little example to illustrate how zip works:

let usernamePublisher = PassthroughSubject<String, Never>()
let agePublisher = PassthroughSubject<Int, Never>()

let userPublisher = Publishers.Zip(usernamePublisher, agePublisher)
    .sink { username, age in
        print("Username: \(username), Age: \(age)")
    }

usernamePublisher.send("johndoe") // No output yet
agePublisher.send(30)             // Output: Username: johndoe, Age: 30
usernamePublisher.send("janedoe") // No output yet
agePublisher.send(28)             // Output: Username: janedoe, Age: 28

As you can see, userPublisher waits for both usernamePublisher and agePublisher to emit a value before combining them into a tuple and emitting the result. This way, the values from the different publishers stay in sync based on their order.

If the example above was a CombineLatest one, when we sent the value “janedoe” in the usernamePubliser, it would print “Username: janedoe, Age: 30”, which maybe be a wrong output depending on your goals.

 

Common Pitfalls using Merge, CombineLatest, and Zip Operators in Combine

Here are the most common problems developers face when using the merge, combineLatest, and zip operators in Combine for iOS:

Merge Operator Problems

  1. Ordering: Since merge emits values as they arrive from the either publisher, it can be challenging to maintain a specific order of values when combining two data streams. If the order is important, you might need to look for alternative solutions or handle the ordering issue separately.

  2. Error Handling: If one of the publishers fails with an error, the merged publisher will also fail, potentially stopping the other publisher’s stream as well. You may need to handle errors individually before merging the publishers.

 

CombineLatest Operator Problems

  1. Initial Values: combineLatest doesn’t emit any values until all publishers have emitted at least one value. If one publisher takes significantly longer to emit its first value, it could cause delays in processing the combined stream.

  2. Frequent Updates: Since combineLatest emits a new tuple whenever any of the publishers emit a value, it can result in a high frequency of updates. This might be problematic if the subscriber’s processing is slow or resource-intensive.

 

Zip Operator Problems

  1. Mismatched Publishers: The zip operator requires all publishers to emit values at the same pace. If one publisher is slower than the others, it can cause the combined stream to stall until all publishers emit their next value. This can lead to performance issues.

  2. Value Synchronization: If one of the publishers has already emitted multiple values before another publisher emits its first value, the zip operator will use the first value from the faster publisher, which might not be the intended behavior. In such cases, you might need to buffer or synchronize values differently.

 

Each of these operators has its unique set of challenges, but being aware of these issues can help you make informed decisions about when to use them and how to handle potential problems in your Combine-based projects.

 

Merge vs. CombineLatest vs. Zip: Which One to Choose to Use Multiple Streams 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 the 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.

And that’s all for today!

 

Conclusion – Handling Various Streams using Combine in Swift

And there you have it, folks! We’ve covered the ins and outs of the merge, combineLatest, and zip operators in Combine for iOS. These operators are indispensable tools in your reactive programming toolkit, and with this guide, you’ll be well on your way to mastering the art of working with asynchronous events and data streams in your iOS projects.

So, whether you’re merging publishers into a harmonious stream, keeping up-to-date with the latest values from multiple sources, or synchronizing values like a pro, Combine’s got your back. Now go forth and conquer those data streams with confidence!

Fellow Apple Developers, that’s all.

I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or say hello on Twitter. I’m available on LinkedIn or send me an e-mail through the contact page.

You can likewise sponsor this blog so I can get my blog free of ad networks.

Thanks for the reading and… That’s all folks.

Image credit: Featured Painting