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.

PublishSubject caches error, won't emit onNext

See original GitHub issue

Not sure if this is intended behavior, but when using a PublishSubject (and any other subject) the error seems to be cached, so each time I subscribe to the subject it propagates that error again. Additionally, subsequent calls to onNext are ignored.

PublishSubject<String> subject = PublishSubject.create();
subject.onError(new RuntimeException());
subject.subscribe(
        System.out::println,
        System.err::println
);
subject.onNext("Hello World!"); // never emitted

If this is intended behavior, how would I go about implementing something that emits errors but will continue normally afterwards?

I’ve resolved in the mean time to using two subjects: one that emits the onNext, one that emits the onError:

PublishSubject<String> successSubject = PublishSubject.create();
PublishSubject<Throwable> errorSubject = PublishSubject.create();
observable.subscribe(
    successSubject::onNext,
    errorSubject::onNext
);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
jrobinson3k1commented, Jul 30, 2015

@akarnokd doing that doesn’t seem much better, since now your type parameter would need to be Object rather than the expected type. It’s a murky solution either way. What would be nice is a way to reset an Observable after it has been terminated, either by onComplete or onError, by just putting it back in its initial state, but keep its subscribers. This can be done manually, but an Rx solution would be handy.

It sounds like this flies in the face of the designed contracts, though.

0reactions
richardtopcommented, Apr 26, 2018

@akarnokd thanks for the comment, I’ve actually using RxSwift and was comparing the Rx interface with more usual Promises or Futures interfaces. They look a bit nicer at the call site, as I may want just propagate the error further, but handle Success a bit differently.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJava observables not emitting events - Stack Overflow
According to your code, it's seem that you need a Subject (like you mention : PublishSubject ) to emit value from different user...
Read more >
PublishSubject (RxJava Javadoc 3.1.5) - ReactiveX
A Subject that emits (multicasts) items to currently subscribed Observer s and terminal events to current or late Observer s. This subject does...
Read more >
RxJava threading: when subscribeOn doesn't work - Medium
Let's recall how a BehaviorSubject works: for each onNext called on the subject, it will notify all current subscribers. At the same time...
Read more >
PublishSubject (RxJava Javadoc 2.1.6)
Notifies the Observer that the Observable has experienced an error condition. If the Observable calls this method, it will not thereafter call Observer.onNext(T) ......
Read more >
monix.reactive.subjects.PublishSubject
If the source terminates with an error, the PublishSubject will not emit ... Caches the emissions from the source Observable and replays them...
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