What is the difference between flatmap and switchmap in RxJava?

后端 未结 7 1783
忘掉有多难
忘掉有多难 2020-11-29 15:07

The rxjava doc definition of switchmap is rather vague and it links to the same page as flatmap. What is the difference between the two operators?

7条回答
  •  渐次进展
    2020-11-29 15:52

    Here is the one more - 101 line long example. That explains the thing for me.

    Like was said: it gets the last observable (the slowest one if you will) and ignores the rest.

    As a result:

    Time | scheduler | state
    ----------------------------
    0    | main      | Starting
    84   | main      | Created
    103  | main      | Subscribed
    118  | Sched-C-0 | Going to emmit: A
    119  | Sched-C-1 | Going to emmit: B
    119  | Sched-C-0 | Sleep for 1 seconds for A
    119  | Sched-C-1 | Sleep for 2 seconds for B
    1123 | Sched-C-0 | Emitted (A) in 1000 milliseconds
    2122 | Sched-C-1 | Emitted (B) in 2000 milliseconds
    2128 | Sched-C-1 | Got B processed
    2128 | Sched-C-1 | Completed
    

    You see the A got ignored.

提交回复
热议问题