Delphian
Delphian

Reputation: 1760

RxJava2 concat() runs only the first Observable

I have 3 methods which return Observable(String). I need to perform this methods one by one. For each method neccessary approximately 2 or 3 minutes. I am using the next code:

Observable.concat(one(),two(),three())
          .subscribeWith(new DisposableObserver<String>() {
                    @Override
                    public void onNext(String s) {
                        LOG.debug(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        LOG.error(e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

Only the first method is performed. The second and the third method are ignored. I can't understand what is the problem? I am using RxJava2 version 2.1.6

Upvotes: 2

Views: 2345

Answers (2)

azizbekian
azizbekian

Reputation: 62189

Apparently, one() is an Observable which does not emit onComplete() event, which means, that it has not yet finished, whereas concat() operator will wait until observable emits a terminal event and only then will start with the next observable.

If you do not possess the creation logics of first observable, you convert the observable into a Single using Single.fromObservable() API.

Observable.concat(Single.fromObservable(one()), two(), three())

If you do have access to the creation logics of first observable, then emit a terminal event (i.e. emitter.onComplete()).


As outlined by David Karnok in the comments sections:

Single.fromObservable() also requires a completing source so it can error you if that source happens to have no items.

Upvotes: 4

cwbowron
cwbowron

Reputation: 1025

Is your first observable completing or is only emitting an item? According to the docs it won't emit items from two or three until after one completes.

Upvotes: 2

Related Questions