Last active
May 11, 2017 04:47
-
-
Save Guang1234567/0a1e17df0ace8d9a3e72b53be5ec20cd to your computer and use it in GitHub Desktop.
How to use **onErrorResumeNext** operator of Rxjava2 ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RxView.clicks(mIvTestGlide) // 只发出 onNext 事件的事件源 | |
.flatMap(new Function<Object, ObservableSource<Object>>() { | |
@Override | |
public ObservableSource<Object> apply(Object o) throws Exception { | |
// another observable which can throw onError. | |
return Observable.error(new Exception("模拟发生错误的地方!!!")) | |
.doOnError(new Consumer<Throwable>() { | |
@Override | |
public void accept(Throwable throwable) throws Exception { | |
// TODO : 由于下面的 onErrorResumeNext, 因此 error 事件无法传递到"订阅者 observer", 故需要在这里做处理. | |
Log.d("CLICK", "button clicked occupy error!!!");//here no errors should occur | |
} | |
}) | |
.onErrorResumeNext(Observable.empty());//prevent observable from terminating | |
//.onErrorResumeNext(Observable.nevver());//prevent observable from terminating | |
} | |
}) | |
.subscribe( | |
new Consumer<Object>() { | |
@Override | |
public void accept(Object o) throws Exception { | |
Log.d("CLICK", "button clicked onNext");//<------ 没有出错的时候,执行此句 | |
} | |
}, | |
new Consumer<Throwable>() { | |
@Override | |
public void accept(Throwable t) throws Exception { | |
Log.d("CLICK", "button clicked onError"); // <------ 这句因为上面的 onErrorResumeNext 故不会执行到此句 | |
} | |
}, | |
new Action() { | |
@Override | |
public void run() throws Exception { | |
Log.d("CLICK", "button clicked onComplete"); // <------ Observable.empty() 是没有 onNext 但有 onComplete, 故会执行到此句 | |
// 实际上不会执行此句, | |
// 1)由于 FlatMap$MergeObserver 因为它里面有段逻辑, 在RxView.clicks(mIvTestGlide) 只发出 onNext 事件的前提下, 会忽略响应 Observable.empty() 的 onComplete 事件. | |
// 2) 所以上面 Observable.empty() 换成 Observable.never()也无所谓. 只不过 empty() 是被主动忽略的, 而 never() 是啥都不干,连被忽略的机会都没有,效率更高(针对这个问题) | |
} | |
}); | |
///------------------------------------- 下面的写法是错的 ---------------------------------------- | |
RxView.clicks(mIvTestGlide) // 只发出 onNext 事件的事件源 | |
.flatMap(new Function<Object, ObservableSource<Object>>() { | |
@Override | |
public ObservableSource<Object> apply(Object o) throws Exception { | |
// another observable which can throw onError. | |
return Observable.error(new Exception("模拟发生错误的地方!!!")); | |
} | |
}) | |
.doOnError(new Consumer<Throwable>() { | |
@Override | |
public void accept(Throwable throwable) throws Exception { | |
// TODO : 由于下面的 onErrorResumeNext, 因此 error 事件无法传递到"订阅者 observer", 故需要在这里做处理. | |
Log.d("CLICK", "button clicked occupy error!!!");//here no errors should occur | |
} | |
}) | |
.onErrorResumeNext(Observable.never())//prevent observable from terminating | |
.subscribe( | |
new Consumer<Object>() { | |
@Override | |
public void accept(Object o) throws Exception { | |
Log.d("CLICK", "button clicked onNext");//<------ 没有出错的时候,执行此句 | |
} | |
}, | |
new Consumer<Throwable>() { | |
@Override | |
public void accept(Throwable t) throws Exception { | |
Log.d("CLICK", "button clicked onError"); // <------ 这句因为上面的 onErrorResumeNext 故不会执行到此句 | |
} | |
}, | |
new Action() { | |
@Override | |
public void run() throws Exception { | |
Log.d("CLICK", "button clicked onComplete"); // <------ Observable.empty() 是没有 onNext 但有 onComplete, 故会执行到此句 | |
// 实际上不会执行此句, | |
// 1)由于 FlatMap$MergeObserver 因为它里面有段逻辑, 在RxView.clicks(mIvTestGlide) 只发出 onNext 事件的前提下, 会忽略响应 Observable.empty() 的 onComplete 事件. | |
// 2) 所以上面 Observable.empty() 换成 Observable.never()也无所谓. 只不过 empty() 是被主动忽略的, 而 never() 是啥都不干,连被忽略的机会都没有,效率更高(针对这个问题) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment