Skip to content

Latest commit

 

History

History
98 lines (53 loc) · 2.05 KB

advanced-flows.md

File metadata and controls

98 lines (53 loc) · 2.05 KB

Advanced Flows

In this chapter we will understand operators like flatmap and others operator that can save us when we are dealing with complicated tasks. how we can combine two asynchronous actions and then move to the next step only if certain conditions are satisfied.

We will cover sereval core methods that Rxusers must know, such as following:

  • .map()
  • .switchMap()
  • .flatMap()
  • concat()
  • .zip()
  • merge()
  • filter()
  • combineLatest()
  • firstElement()
  • and()
  • buffer()
  • cache()
  • cast()
  • defer()
  • distinctUntilChange()

this chapter will also cover:

  • What is Observable unrwapping and how to use it
  • How to pass values between different flow of the Observable
  • How to combine items from multiple Observer flows

Let's start with** Observable unwrapping**, it's not a rare case when we need to create and consume nested Observables. let's say we have Registration process and we need to use the Token to retrieve the user details throw another observable.

wrap everything into one big flow sound like a good idea, in cases like we often use tools like .flatMap(). later we will dive into the .flatMap and explain in more detail.

However before we dive to 30 meters lets start with the little brother called map that is easier to grasp.

Map

The idea behind map is to transform one item to another by applying a function to each item.

Observable.just("First", "Second")
             .map { it.toInt }

This will convert all items to integers and change the flow type. To summarize .map() takes an item and returns a different to be processed down the stream. the new type can be different type, different value or stay the same. let's say we just want to convert the items to upper case:

Observable.just("First", "Second")
             .map { it.toUpperCase() }
  • create better photo

![](/assets/Screen Shot 2017-11-02 at 11.45.54.png)

SwitchMap

FlatMap

Concat

Zip

Merge

Filter

CombineLatest

FirstElement

And

Buffer

Cache

Cast

Defer

DistinctUntilChange