在 rxjava 2 中找不到 OnErrorThrowable
OnErrorThrowable not found in rxjava 2
我正在将 rxjava 1 升级到 rxjava 2。rxjava 1 中有 OnErrorThrowable,但 rxjava 2 中没有。我可以在以下代码中做什么来替换 OnErrorThrowable?
static Observable<String> sampleObservable() {
return Observable.defer(new Callable<ObservableSource<String>>() {
@Override
public ObservableSource<String> call() throws Exception {
try {
// Do some long running operation
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
} catch (InterruptedException e) {
throw OnErrorThrowable.from(e);
}
Log.d(TAG, "The sleep is over, now produce something");
return Observable.just("one", "two", "three", "four", "five");
}
});
}
您可以 return Observable.error(e);
而不是投入 catch 块。
在 2.x 中,您不必包装异常,因为所有函数类型都声明 throws Exception
:
static Observable<String> sampleObservable() {
return Observable.defer(new Callable<ObservableSource<String>>() {
@Override
public ObservableSource<String> call() throws Exception {
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
Log.d(TAG, "The sleep is over, now produce something");
return Observable.just("one", "two", "three", "four", "five");
}
});
}
我正在将 rxjava 1 升级到 rxjava 2。rxjava 1 中有 OnErrorThrowable,但 rxjava 2 中没有。我可以在以下代码中做什么来替换 OnErrorThrowable?
static Observable<String> sampleObservable() {
return Observable.defer(new Callable<ObservableSource<String>>() {
@Override
public ObservableSource<String> call() throws Exception {
try {
// Do some long running operation
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
} catch (InterruptedException e) {
throw OnErrorThrowable.from(e);
}
Log.d(TAG, "The sleep is over, now produce something");
return Observable.just("one", "two", "three", "four", "five");
}
});
}
您可以 return Observable.error(e);
而不是投入 catch 块。
在 2.x 中,您不必包装异常,因为所有函数类型都声明 throws Exception
:
static Observable<String> sampleObservable() {
return Observable.defer(new Callable<ObservableSource<String>>() {
@Override
public ObservableSource<String> call() throws Exception {
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
Log.d(TAG, "The sleep is over, now produce something");
return Observable.just("one", "two", "three", "four", "five");
}
});
}