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.
Transforming Operators in RxSwift: map, flatMap & flatMapLatest
Written by
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.
1. map()
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:
2. flatMap()
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.
3. flatMapLatest()
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.
Wrapping up
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.
Share this post
Facebook
Twitter
LinkedIn
Reddit
You may also like
Process
Eliminate Crashes & Incidents in Mobile Development Teams
Defect management should not be a phase in the lifecycle of an app, but instead a fundamental part of an agile process. Here is my recommendation of processes to enforce defect management into your team’s daily workflow.
DevOps
Architecting a Feature Toggle Service for iOS Apps
Feature toggling is essential for keeping your apps flexible in production. Sometimes you want to try ideas fast to see if they stick. Maybe you want to split test two ideas and see what performs the best. Whatever the case may be, the feature toggling service should be highly scalable and not be a bottleneck in your workflow. Here’s how you create a feature toggling service with a clean iOS architecture.
Automation
Continuous Integration Using GitHub Actions for iOS Projects
Github Actions are finally publicly released! It’s an opportunity to easily enable continuous integration in your projects on GitHub, so here’s how you set it up for your iOS projects to perform automated code validation and testing on pull requests.