mtyson mtyson - 11 months ago 77
Java Question

RxJava: Dynamic set of observables

I have a central class, call it

. It can have 1-N observables added to it. These I need to add dynamically, and then know when the final onComplete() has executed.

How to do this?

Code sample:

public class Central {
public void addObservable(Observable o){
// Add the new observable to subscriptions

Answer Source

Use a PublishSubject:

PublishSubject<Observable<T>> subject = 
subject.concatMap(o -> 
      o.doOnCompleted(() -> System.out.println("inner completed")))
    .doOnCompleted(() -> System.out.println("completed"))

subject.onNext(Observable.just(t2, t3));