Rxjava: what is the diference between flatmap and concatmap?

Created on 29 Apr 2015  路  7Comments  路  Source: ReactiveX/RxJava

  1. what is the diference between flatmap and concatmap?
  2. `A.concatmap(b).subscribe(subscriber)``, if the b call the onComplete, subsriber's onComplete will not be invoked? why? the same as flatmap.
Question

Most helpful comment

Marble diagrams showing the difference:

concatMap is sequential: http://reactivex.io/RxJava/javadoc/rx/Observable.html#concatMap(rx.functions.Func1)
flatMap is concurrent: http://reactivex.io/RxJava/javadoc/rx/Observable.html#flatMap(rx.functions.Func1)

flatmap

concatmap

Note the interleaving on flatMap.

All 7 comments

  1. FlatMap may interleave emissions of Observables while ConcatMap will save order of emissions
  1. concatMap 'runs' only a single source Observable at a time whereas flatMap subscribes to many of them and if those Observables are async, you get async execution and interleaved emissions between Observables, but values from each of the Observables retain the relative order to each other.
  2. if you produce normal, terminating observables in the function b and A is also terminating properly, then you should get an onCompleted() event. Do you have a test case which exhibits the behavior you mentioned?

Marble diagrams showing the difference:

concatMap is sequential: http://reactivex.io/RxJava/javadoc/rx/Observable.html#concatMap(rx.functions.Func1)
flatMap is concurrent: http://reactivex.io/RxJava/javadoc/rx/Observable.html#flatMap(rx.functions.Func1)

flatmap

concatmap

Note the interleaving on flatMap.

Oh, I know, concat map is A->onNext->onNext->onNext->onComplete->B->onNext->onNext->onNext->onComplete, but flatmap is
A->onNext->onNext->onNext->onComplete
| | | |
B->onNext->onNext->onNext->onComplete

A.concatmap(b).subscribe(subscriber)`, if the b call the onComplete, subsriber's onComplete will not be invoked? why? the same as flatmap.

The completion of b should invoke onCompleted(). do you have a test case that demonstrates your problem?

Closing due to inactivity.

Thank You. It helped me to understand difference :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dsvoronin picture dsvoronin  路  4Comments

midnight-wonderer picture midnight-wonderer  路  3Comments

Jaap-van-Hengstum picture Jaap-van-Hengstum  路  3Comments

philleonard picture philleonard  路  3Comments

nltran picture nltran  路  4Comments