android - 实时数据 react 流 : How to handle errors while using fromPublisher()

标签 android rx-java2 android-livedata reactive-streams

我正在尝试使用 react 流将“Flowable”转换为“LiveData”。查阅了很多文章,但我仍然没有掌握处理错误状态的概念。目前,我正处于学习RxJava的开始阶段。如果有人能为我提供关于如何处理这个问题的详细解释,那将非常有帮助。

Repository.java

    public class Repository {

    private static final String API_KEY = "";
    private static final String TAG = "Repository class- ";

    private MovieService _movieApi;
    private MediatorLiveData<MovieJsonData> _movieList;
    private static Repository _repositoryInstance;

    public static Repository getInstance(){
        if(_repositoryInstance == null){
            return new Repository();
        }
        return _repositoryInstance;
    }

    private Repository(){
        _movieApi = MovieClient.getTrendingMovieClient().create(MovieService.class);
        _movieList = new MediatorLiveData<>();
    }

    public MediatorLiveData<MovieJsonData> fetchData(){

        LiveData<MovieJsonData> _source = LiveDataReactiveStreams.fromPublisher(
                _movieApi.getTrendingMovies(API_KEY)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnError(error -> Log.d(TAG, "fetchData: " + error.getMessage()))
        );

        _movieList.addSource(_source, new Observer<MovieJsonData>() {
            @Override
            public void onChanged(MovieJsonData movieJsonData) {
                _movieList.setValue(movieJsonData);
            }
        });

        return _movieList;
    }}

错误堆栈跟踪:

    D/Repository class-: fetchData: timeout 
    W/System.err:io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to 
    the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to 
    begin with.
    Caused by: java.lang.RuntimeException: LiveData does not handle errors. Errors from publishers should 
    be handled upstream and propagated as state

最佳答案

很简单:LiveData 不处理错误。你处理它们。

实时数据比响应式(Reactive)可观察数据简单得多。实时数据负责交付 100% 完整的对象。就是这样。

如果您查看 LiveDataReactiveStreams.fromPublisher ( here ) 的文档,您会发现明确指出实时数据不处理错误:

Note that LiveData does NOT handle errors and it expects that errors are treated as states in the data that's held. In case of an error being emitted by the publisher, an error will be propagated to the main thread and the app will crash.

为了避免崩溃,请使用onErrorReturn:

 LiveData<MovieJsonData> _source = LiveDataReactiveStreams.fromPublisher(
            _movieApi.getTrendingMovies(API_KEY)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .onErrorReturn(error -> EmptyMovieJsonData())
    );

关于android - 实时数据 react 流 : How to handle errors while using fromPublisher(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62780674/

相关文章:

java - 可热完成长时间运行的任务

Android:如何在不观察的情况下从 LiveData 获取值(value)

android - 组合两个具有相同观察者类型的 LiveData 对象

android - 在 PreferenceActivity 中的首选项更改时在 Android 主 Activity 中使用react

android - Dimen 属性在德语 android 中不起作用

Android - 调用 System.loadLibrary() 导致进程终止

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

android - 何时在 Android 中使用 RxJava,何时使用 Android Architectural Components 中的 LiveData?

使用 Flow 时 Android 房间查询为空

android - 通过 Android 应用程序在 Facebook 上发布消息