本文介绍了架构组件改造和RxJava 2错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用来自改造和Okhttp的API请求在体系结构组件中实现新的ViewModel,一切正常,但我不知道如何将错误响应从改造传递给LiveDataReactiveStreams.fromPublisher,然后传递给上游片段中的观察者.这是我到目前为止的内容:

I am currently trying to implement the new ViewModels in the architecture components with an API request from retrofit and Okhttp, everything is working but I can't figure out how to pass an error response from retrofit to LiveDataReactiveStreams.fromPublisher and then upstream to the observer in the fragment. This is what I have so far:

public class ShowListViewModel extends AndroidViewModel {

private final ClientAdapter clientAdapter;

private LiveData<List<Show>> shows;

public ShowListViewModel(Application application) {
    super(application);
    clientAdapter = new ClientAdapter(getApplication().getApplicationContext());

    loadShows();
}

public LiveData<List<Show>> getShows() {
    if (shows == null) {
        shows = new MutableLiveData<>();
    }

    return shows;
}

void loadShows() {
    shows = LiveDataReactiveStreams.fromPublisher(Observable.fromIterable(ShowsUtil.loadsIds())
            .subscribeOn(Schedulers.io())
            .flatMap(clientAdapter::getShowWithNextEpisode)
            .observeOn(Schedulers.computation())
            .toSortedList(new ShowsUtil.ShowComparator())
            .observeOn(AndroidSchedulers.mainThread())
            .toFlowable());
}

然后在片段中,在OnCreate中使用以下命令设置viewModel:

And in the fragment I setup the viewModel with the following in OnCreate:

ShowListViewModel model = ViewModelProviders.of(this).get(ShowListViewModel.class);
    model.getShows().observe(this, shows -> {
        if (shows == null || shows.isEmpty()) {
            //This is where we may have empty list etc....
        } else {
            //process results from shows list here
        }

    });

一切正常,但是当前,如果我们处于脱机状态,则改造会抛出runtimeException并崩溃.我认为问题出在这里:

Everything works as expected but currently if we are offline then retrofit is throwing a runtimeException and crashing. I think the problem lies here:

LiveDataReactiveStreams.fromPublisher(Observable.fromIterable(ShowsUtil.loadsIds())
            .subscribeOn(Schedulers.io())
            .flatMap(clientAdapter::getShowWithNextEpisode)
            .observeOn(Schedulers.computation())
            .toSortedList(new ShowsUtil.ShowComparator())
            .observeOn(AndroidSchedulers.mainThread())
            .toFlowable());
}

通常,我们将使用rxjava2 subscription并从那里进行改型来捕获错误,但是当使用 LiveDataReactiveStreams.fromPublisher ,它为我们订阅了flowable.那么我们如何将这个错误传递给这里:

Normally we would use rxjava2 subscribe and catch the error from retrofit there, but when using LiveDataReactiveStreams.fromPublisher it subscribes to the flowable for us. So how do we pass this error into here:

model.getShows().observe(this, shows -> { //process error in fragment});

推荐答案

您不仅需要通过LiveData对象公开展示列表,还需要将展示和错误包装到可以容纳该错误的类中.

Rather than exposing just the list of shows through your LiveData object you would need to wrap the shows and error into a class that can hold the error.

通过您的示例,您可以执行以下操作:

With your example you could do something like this:

    LiveDataReactiveStreams.fromPublisher(Observable.fromIterable(ShowsUtil.loadsIds())
            .subscribeOn(Schedulers.io())
            .flatMap(clientAdapter::getShowWithNextEpisode)
            .observeOn(Schedulers.computation())
            .toSortedList(new ShowsUtil.ShowComparator())
            .observeOn(AndroidSchedulers.mainThread())
            .map(Result::success)
            .onErrorReturn(Result::error)
            .toFlowable());

其中Result是包含错误或结果的包装器类

Where Result is the wrapper class that holds either the error or result

final class Result<T> {

    private final T result;
    private final Throwable error;

    private Result(@Nullable T result, @Nullable Throwable error) {
        this.result = result;
        this.error = error;
    }

    @NonNull
    public static <T> Result<T> success(@NonNull T result) {
        return new Result(result, null);
    }

    @NonNull
    public static <T> Result<T> error(@NonNull Throwable error) {
        return new Result(null, error);
    }

    @Nullable
    public T getResult() {
        return result;
    }

    @Nullable
    public Throwable getError() {
        return error;
    }
}

这篇关于架构组件改造和RxJava 2错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:54
查看更多