1

我正在将 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");
        }
    });
}
4

2 回答 2

2

你可以返回Observable.error(e);而不是扔进你的 catch 块。

于 2017-01-12T09:19:58.097 回答
2

在 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");
    }
});

}

于 2017-01-12T12:37:25.347 回答