Transforming Operators in RxSwift: map, flatMap & flatMapLatest
In the last post, we looked at various filtering operators to be familiar with to get closer to becoming an RxSwift rockstar. This time, we'll cover perhaps the most important type of operators you'll be working with which is the transforming operators.
The transforming operators are used to model the data emitted by observables to meet the requirements of the subscribers. RxSwift offers a wide range of operators that provide control over data and events in your application. We'll only be focusing on the most commonly used operators.
The first operator on the list is one you'll likely be using the most. It's the map-operator. It works similar to the standard map-operator from Swift, except it works on observables. Here's what you can do with it.
The map-operator lets you apply a function to each element emitted by an observable. The operator will then return an observable that emits the results of the function. It looks like this on a marble diagram:
In this diagram, the observable sequence containing the values 1, 2 and 3 is passed through the map-operator that will multiply each value by 10. It then returns the observable sequence 10, 20, and 30.
Lets put this operator into action in code:
It is possible for an observable sequence to contain observables that you can subscribe to. In that case, the flatMap-operator is a lifesaver used to reach into each element of a sequence and deal with the inner observable sequence that may be contained in the elements.
In the marble diagram above, the input (upper line) is an observable sequence containing observables that are represented with circles. If we look at the output (lower line), the elements emitted by the input observables, represented with squares, are collected in a single observable sequence. In other words, the flatMap-operator removes the multiple levels of nested observables and instead flattens them into a single observable sequence. Much easier to work with.
Now that we got the "flattening"-part down, I'll remind you that it contains "map" in its name because it's also possible to apply a function, just like the map-operator from earlier. The function can be used to modify the observable sequence being outputted. This is similar to the map-operator but looks slightly different in code. Here's how it looks:
Notice how the flatMap-operator monitors for changes on each observable at once as seen when John gets a new car which gets printed. The next operator is useful if you only want to receive updates from the most recent observable and unsubscribe from old ones.
As the name suggests, the flatMapLatest-operator shares many similarities with the flatMap-operator. The difference between them is that flatMapLatest only produces elements from the most recent observable. This means that flatMapLatest switches its monitoring to the newest observable and ignores emissions from previous observables.
Let's see this in action using the previous example:
What's worth noticing here is that the update regarding John's new car is never printed because John is no longer the latest observable - instead, Alice is the latest observable.
We looked at the most common transforming 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.