PublishSubject caches error, won't emit onNext
See original GitHub issueNot 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:
- Created 8 years ago
- Comments:9 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
@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.