RxSwift Transforming Operators: map, flatMap, flatMapLatest and concatMap

Aaina jain
Aug 27, 2018 · 4 min read

Throwing light over commonly used operators

Image for post
Image for post
Credit: unsplash.com

map:

The Map operator applies a closure to each item emitted by the source Observable, and returns an Observable that emits the results of applied callback.

Image for post
Image for post
RxMarbles: map

In general, When you have an item emitted from observable and you want to transform them into other type but not an observable then map is preferred.

map over array has following characteristics:

  • It takes a closure as an argument,

The only difference in using map() on Observables are that:

  • It returns a new Observable instead of returning a new array.
Input: 1—————2————3—————|→Result: “1”————“2“—————“3”—————|→
transformation using map operator

flatMap:

The flatMap operator transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable.

It applies a closure to each item emitted by the source Observable and returns an Observable that itself emit items.

Image for post
Image for post
RxMarbles: flatMap
Input: 1 —— 2 —— 3 —— |→Intermediate:Observable(“1”)—————|→Observable(“2”)—————|→Observable(“3”)————|→Result: “1"————“2”————“3”————|→
transformation using flatMap operator

In general, what flatMap does is it applies a closure to each emitted Observable and returns an Observable. It internally subscribes to each of these Observable, merges them and finally results in an flatten array.

Note that FlatMap merges the emissions of these Observables, so that they may interleave.

FlatMap operator doesn’t care about the order of the items. It creates a new observable for each item and that observable lives its own life. Some of them will emit faster, and others slower because we delay each of them for a random amount of seconds.

Here is a simple thumb-rule to decide as when to use flatMap() over map() in Rx's Observable.

If what you're returning as an end result of your transformation is:

  • a non-observable object then you’d use just map(). map() wraps that object in an Observable and emits it.

Another example of flatMap:

Image for post
Image for post
Item emitted from inner observable even when newer observable started emitting elements

flatMapLatest:

The only difference between flatMap and flatMapLatest is it disposes previous subscription when new subscription happens.

Image for post
Image for post

Whenever a new item is emitted by the source Observable, it will unsubscribe to and stop mirroring the Observable that was generated from the previously-emitted item, and begin only mirroring the current one.

Image for post
Image for post

concatMap:

Map values to inner observable, subscribe and emit in order. The only difference between flatMapLatest and concatMap is it preserves the order of items. It has one flow also, not good for asynchronous operations as waits for observable to finish all items in sequence.

Image for post
Image for post
Image for post
Image for post

You can catch me at:

Linkedin: Aaina Jain

Twitter: __aainajain

If you have any suggestions for the next post write to me at aainajain100@gmail.com.

Swift India

Swift Developer Community in India 🇮🇳

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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