The 5 Most Important Combining Operators in RxSwift
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.
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:
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:
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:
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:
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:
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.
Share this post
You may also like
Here’s a set of key principles to guide you to do iOS development according to best practices. They are all about the bigger picture of iOS development, including processes and architecture decisions – not simply language specific challenges.
One of the best ways to increase productivity in a react native project is to automate the process of deploying builds you want to share internally in your team or with actual costumers via the app store. And you might be surprised at how easy it can actually be implemented when you have the right set of tools like GitHub Actions and EAS.
Transforming operators in RxSwift serve the purpose of preparing data coming from observables before it reaches the subscribers. Let’s cover this type of operators and look at 3 operators you will be using all the time.