gpt4 book ai didi

android - 架构组件改造和 RxJava 2 错误处理

转载 作者:太空狗 更新时间:2023-10-29 13:51:36 25 4
gpt4 key购买 nike

我目前正在尝试通过改造和 Okhttp 的 API 请求在架构组件中实现新的 ViewModel,一切正常,但我不知道如何将改造的错误响应传递给 LiveDataReactiveStreams.fromPublisher 然后向上游到 fragment 中的观察者。这是我目前所拥有的:

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());
}

在 fragment 中,我在 OnCreate 中使用以下内容设置了 viewModel:

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 并崩溃。我认为问题出在这里:

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

通常我们会使用 rxjava2 subscribe 并从那里的改造中捕获错误,但是当使用 LiveDataReactiveStreams.fromPublisher 时它为我们订阅了 flowable。那么我们如何将这个错误传递到这里:

model.getShows().observe(this, shows -> {//fragment 处理错误});

最佳答案

与其仅通过 LiveData 对象公开节目列表,不如将节目和错误包装到一个可以保存错误的类中。

根据您的示例,您可以执行以下操作:

    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 是包含错误或结果的包装类

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;
}
}

关于android - 架构组件改造和 RxJava 2 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46304042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com