The 5 Most Important Combining Operators in RxSwift

RxSwift provides plenty of options for joining your observable sequences together. Let's go over the 5 most important combining operators you'll likely use the most.

Written by 

In the last post, we covered several transforming operators in RxSwift you should familiarise yourself with. Make sure to read it, if you haven't done so. This time, we'll go over the third and final type of operator in this 3-part mini series on RxSwift operators. That is the combining operators.

The combining operators serve the purpose of joining observable sequences together in various ways. We'll cover how to work with the 5 most important combining operators that I think you'll find most useful.

1. merge()

The first operator on the list is merge. It should be pretty self-explanatory. It lets you merge the emissions from multiple observables. This output acts like a single observable.

In the marble diagram above, we see that each emission from either one of the source observables are outputted in the target observable as they happen. The implementation of this is fairly straight forward:

2. concat()

concat is similar to the merge-operator. It's used to combine multiple observables, but the way it differs in is that all emissions by the first source observable come before any emissions from the second source observable (and so forth, if there are more observables).

You can see this behaviour in the marble diagram above. What happens is that the concat-operator waits to subscribe to the next observable until the previous observable completes.

Note that there is variant of the concat-operator called startWith that prepends observable items rather than appending them. You may want to check that one out as well.

Let's now look at a code example of the concat-operator:

3. combineLatest()

The next one is combineLatest. It is used to output a value that depends on items from different observables. When an item is emitted by either one of the two source observables, the operator takes the latest values from both of the source observables. The items are then passed through a closure which determines how to combine them.

Let's just get right into the code example:

4. zip()

You may find it annoying that the combineLatest repeats much of the same elements as it doesn't wait for all source observables to provide a new item. Well, you're in luck, because that's exactly what zip does. You also provide a closure to this operator, similar to combineLatest, that specifies how to combine the items.

Using this operator, we can match up the x-men movies and IMDB ratings from the previous example correctly. The implementation would look like this:

5. amb()

Finally, we have amb that allows you to listen for emissions from multiple observables at ones. But as soon as one of the observables emits a value, the operator will ignore and discard emission from all other observables.

Let's look at a code example of the amb-operator:

Wrapping up

We looked at the most common combining operators that RxSwift has to offer. You can find the entire list of operators available here.

Thanks for reading. Leave a cover to let me know of other RxSwift related topics you want me to cover.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.