問(wèn)題描述
最近在使用RxJava時(shí),出現(xiàn)了一個(gè)錯(cuò)誤悼沈,錯(cuò)誤日志如下:
02-28 09:42:16.962 16647-17731/com.aspire.mpus E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1
Process: com.aspire.mpus, PID: 16647
java.lang.Throwable: 網(wǎng)絡(luò)連接超時(shí)
at com.aspire.mpus.core.api.Api.onError(Api.java:84)
at com.aspire.mpus.core.api.Api.access$100(Api.java:23)
at com.aspire.mpus.core.api.Api$1.subscribe(Api.java:116)
at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
at io.reactivex.Scheduler$1.run(Scheduler.java:134)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
拋出異常的原因是我在onError方法中會(huì)指定一些錯(cuò)誤的原因嘲叔,例如網(wǎng)絡(luò)連接超時(shí)、連接服務(wù)器失敗等蜒犯。
private void onError(Exception e, ObservableEmitter emitter, String message) {
e.printStackTrace();
LogUtils.error(e);
if (e instanceof SocketTimeoutException) {
emitter.onError(new Throwable("網(wǎng)絡(luò)連接超時(shí)"));
} else if (e instanceof ConnectException) {
emitter.onError(new Throwable("連接服務(wù)器失敗"));
} else {
emitter.onError(new Throwable(message));
}
emitter.onComplete();
}
解決方法
判斷emitter是否被取消訂閱组橄。如果當(dāng)前的emitter未被取消訂閱荞膘,則正常拋出異常,否則不執(zhí)行玉工。
private void onError(Exception e, ObservableEmitter emitter, String message) {
e.printStackTrace();
LogUtils.error(e);
if (!emitter.isDisposed()) {
if (e instanceof SocketTimeoutException) {
emitter.onError(new Throwable("網(wǎng)絡(luò)連接超時(shí)"));
} else if (e instanceof ConnectException) {
emitter.onError(new Throwable("連接服務(wù)器失敗"));
} else {
emitter.onError(new Throwable(message));
}
emitter.onComplete();
}
}
重現(xiàn)步驟
- 開(kāi)始一個(gè)網(wǎng)絡(luò)請(qǐng)求
- 在請(qǐng)求未結(jié)束時(shí)結(jié)束當(dāng)前頁(yè)面
- 請(qǐng)求結(jié)束后羽资,如果未能請(qǐng)求成功,則onError方法中會(huì)判斷異常信息遵班,并拋出異常屠升,但是Observable未能捕捉到異常,最終導(dǎo)致APP crash
Observable生命周期管理
在執(zhí)行網(wǎng)絡(luò)請(qǐng)求時(shí)狭郑,會(huì)生成一個(gè)Disposable對(duì)象腹暖,該對(duì)象保存了Observable的狀態(tài)(訂閱、取消訂閱)翰萨。
查看ObservableEmitter源碼脏答,
public interface ObservableEmitter<T> extends Emitter<T> {
/**
* Sets a Disposable on this emitter; any previous Disposable
* or Cancellation will be unsubscribed/cancelled.
* @param d the disposable, null is allowed
*/
void setDisposable(@Nullable Disposable d);
/**
* Sets a Cancellable on this emitter; any previous Disposable
* or Cancellation will be unsubscribed/cancelled.
* @param c the cancellable resource, null is allowed
*/
void setCancellable(@Nullable Cancellable c);
/**
* Returns true if the downstream disposed the sequence.
* @return true if the downstream disposed the sequence
*/
boolean isDisposed();
/**
* Ensures that calls to onNext, onError and onComplete are properly serialized.
* @return the serialized ObservableEmitter
*/
@NonNull
ObservableEmitter<T> serialize();
}
發(fā)現(xiàn)ObservableEmitter也持有Disposable的屬性,所以增加對(duì)當(dāng)前訂閱事件的判斷亩鬼,然后選擇是否拋出異常殖告,即可解決問(wèn)題。