Martynas Jurkus
Martynas Jurkus

Reputation: 9301

How to create Observable based on conditions

Is there a way to create observables based on some conditions?

For example if I have Observable.just(1, 2, 3, ..., n) and if next emitted item is 1, then doSomethingA, if 5 then doSomethignB otherwise doWithEverythingElse.

Or am I asking too much? :)

Upvotes: 0

Views: 461

Answers (1)

Dave Moten
Dave Moten

Reputation: 12087

Use an if statement in doOnNext:

observable.doOnNext(i -> 
    if (i==1)
        doSomethingA();
    else if (i==5)
        doSomethingB();
    else 
        doWithEverythingElse());

Upvotes: 2

Related Questions