question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onNext not called if onError is called immediately after when Scheduler is defined in observeOn

See original GitHub issue

If I set a Scheduler in .observeOn(...) for an Observable and call subscriber.onError immediately after subscriber.onNext, onNext is never called in my Observer.

If I either add a sleep between subscriber.onNext and subscriber.onError or don’t set a Scheduler in .observeOn(...), onNext is called in my Observer.

See example:

Observable.create(new Observable.OnSubscribe<String>() {
    @Override
    public void call(Subscriber<? super String> subscriber) {
        subscriber.onNext("Test");
        subscriber.onError(null);
    }
}).observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                System.out.println("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("onError");
            }

            @Override
            public void onNext(String s) {
                System.out.println("onNext");
            }
        });

// Only prints "onError", but should print "onNext" and then "onError"

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
larryngcommented, Nov 17, 2017

Note to future self because I know I’m going to forget this (again): you can use .observeOn(Scheduler scheduler, boolean delayError) to ensure onNext isn’t skipped by onError.

1reaction
akarnokdcommented, Apr 20, 2015

Something like this:

Observable.create(new Observable.OnSubscribe<String>() {
    @Override
    public void call(Subscriber<? super String> subscriber) {
        subscriber.onNext("Test");
        subscriber.onError(null);
    }
}).materialize().observeOn(AndroidSchedulers.mainThread()).<String>dematerialize()
        .subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                System.out.println("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("onError");
            }

            @Override
            public void onNext(String s) {
                System.out.println("onNext");
            }
        });
Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJava Android onError takes too long to be called
RxJava Android onError takes too long to be called · Could you remove observeOn and use Schedulers.io() for subscribeOn and validate the result ......
Read more >
ObserveOn operator - ReactiveX
Note that ObserveOn will forward an onError termination notification immediately if it receives one, and will not wait for a slow-consuming observer to ......
Read more >
Understanding RxJava subscribeOn and observeOn
This scheduler is expensive as new thread is spawned every time and no reuse happens. Schedulers.from(Executor executor) creates and returns a ...
Read more >
Exploring RxJava in Android
On the other hand, calling the method observeOn , will set up the thread ... By default, if no such Scheduler is provided,...
Read more >
Fundamentals of RxJava with Kotlin for absolute beginners
Let's break it down, just converts the string "Hello Reactive World" to an ... onNext(kind) ... If there's no value, onError will be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found