zimper
zimper

Reputation: 109

RxJava. Call onError after onNext

I want to call onNext and then call the onError method. But I can not do it. Can anybody help me? Example:

Observable.create(new ObservableOnSubscribe<String>() {
    @Override
    public void subscribe(ObservableEmitter<String> e) throws Exception {
        e.onNext("value");
        e.onError(new Exception("exc"));
    }
})
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<String>() {
            @Override
            public void accept(@NonNull String s) throws Exception {
                Log.v("TAG", s);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {
                Log.v("TAG", throwable.getLocalizedMessage());
            }
        });

Output: exc

Thrad.sleep(500) helps me, but i think this is the wrong way

Upvotes: 1

Views: 661

Answers (1)

Kiskae
Kiskae

Reputation: 25573

If you don't specify delayError = true in the observeOn operator then it will end up checking if it terminated (onError was called) before it ends up emitting onNext that was called beforehand.

See documentation:

delayError - indicates if the onError notification may not cut ahead of onNext notification on the other side of the scheduling boundary. If true a sequence ending in onError will be replayed in the same order as was received from upstream

Upvotes: 1

Related Questions