When doing the following in RxJava1, the exception in onNext would be re-routed into the same Subscriber's onError:
Observable.from(Arrays.asList("1", "22", "333", "4444")).subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.d("RxJava1", "onError: " + e.getCause());
}
@Override
public void onNext(String s) {
if (s.length() == 4) {
Integer test = null;
test.hashCode();
}
Log.d("RxJava1", s + " - " + s.length());
}
});
output:
D/RxJava1: 1 - 1
D/RxJava1: 22 - 2
D/RxJava1: 333 - 3
D/RxJava1: onError: null
When doing, as far as I know, the same in RxJava2, this behaviour has changed and no longer returns to onError, but simply crashes:
Observable.fromIterable(Arrays.asList("1", "22", "333", "4444")).subscribeWith(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
if (s.length() == 4) {
Integer test = null;
test.hashCode();
}
Log.d("RxJava2", s + " - " + s.length());
}
@Override
public void onError(Throwable e) {
Log.d("RxJava2", "onError: " + e.getCause());
}
@Override
public void onComplete() {
}
});
Output:
D/RxJava2: 1 - 1
D/RxJava2: 22 - 2
D/RxJava2: 333 - 3
D/AndroidRuntime: Shutting down VM
I'd mostly like to know, which of the 2 versions is doing it "wrong"? Was this a bug in RxJava1 that has been fixed? Is this a bug in RxJava2? Or has this not been a conscious change in the first place, since I can't find any details about this?
ps. I did notice that wrapping this into 'SafeObserver' re-routes to onError again
Neither is wrong, the two versions have different design constraints. In 2.x the Observer/Subscriber can't throw from its onXXX methods.
If you use the 1-2-3 argument subscribe() methods in 2.x you are covered. Otherwise if you really can't fix your onNext use safeSubscribe().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With