Swift Combine: combineLatest() operator

In this article, let’s go ahead and take a look at another combining operator. combineLastest(). This operator is a little bit different. combineLatest() is basically going to combine two different publishers. The great thing is that both of those publishers can have different types. Do you know merge() operator? It’s also combining publishers but merge() requires publishers to have the same type.

combineLastest()’s return value,.

When you are using combineLatest(), what you are going to get as a sequence is tuple. The latest value from each of the publisher is combined together into a tuple.

I created two publishers with two different types. One is getting Integer and another is getting String. You are neither receiving Int nor String, but you are going to receive ((Int, String)) Tuple Value. So, when you send an event from publisher1 alone, it simply doesn’t work. You must have a pair of values from publisher1 and publisher2 when it’s your first time to publisher.

Like this, you must send a pair of values. Then, in sink(), you can receive the tuple. But after publishing the first tuple value with publisher1 and publisher2 , when you publisher a new value, left-side int is also passed down to the downstream.

More generally, the observable produced by combineLatest emits its first value when all the observables first emits a value, and subsequent values are emitted when any of the observables emits a value.

Conclusion

  • combineLatest() combine multiple publishers
  • combineLatest() returns tuple in the sink().
  • combineLatest() needs a paired event.

--

--

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!