pablobaldez
pablobaldez

Reputation: 924

RxJava - Start one Observable when other one finish

I have two Observables with different generic types: Observable o1 and Observable o2

I was defined to o1 the onComplet() and onNext() functions and i want that when this Observable get their finish then the o2 can start.

I tryed the Observable.concat() but they have different types, so this approach doesn't work...

So How can i do this?

Upvotes: 6

Views: 2399

Answers (2)

Dave Moten
Dave Moten

Reputation: 12097

Use castAs before concatWith (and ignoreElements can be useful too):

Observable<T> o1;
Observable<R> o2;

Observable<R> o3 = 
  o1.ignoreElements()
    .castAs(R.class)
    .concatWith(o2);

Or if you're working with generic types (thus can't use R.class):

Observable<R> o3 = ((Observable<R>)(Observable<?>)
  o1.ignoreElements())
    .concatWith(o2);

Upvotes: 7

marstran
marstran

Reputation: 28056

Observable has a method called doOnCompleted which takes an Action0 (a zero-argument function) and returns a new Observable which performs the action when it completes normally. Maybe you can use this to achieve what you want?

o1.doOnCompleted(() -> {
    // Start o2.
});

Upvotes: 0

Related Questions