O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extracting FlatMap

.flatMap() parts of the flow can become really complicated. Often, they contain nested calls to multiple .map(), Observable.just(), and other functions.

This makes the .flatMap() Function interfaces a prime candidate for simplification by method extraction. At the moment, consider the tweet retrieval part (the version that doesn't use for loop to filter tweets):

observeTwitterStream(configuration, filterQuery)        .sample(2700, TimeUnit.MILLISECONDS)        .map(StockUpdate::create)        .flatMapMaybe(update -> Observable.fromArray(trackingKeywords)                .filter(keyword ->                 update.getTwitterStatus().toLowerCase()                .contains(keyword.toLowerCase()))                .map(keyword -> update)                .firstElement()        )

It has such a flow where the following bit can be extracted ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required