Why RxJava with Retrofit on Android doOnError() does not work but Subscriber onError does

旧城冷巷雨未停 提交于 2019-12-21 06:51:19

问题


can someone explain me why code like this:

 networApi.getList()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(throwable -> {
                throwable.getMessage();
            })
            .doOnNext(list -> {
                coursesView.populateRecyclerView(list);
                courseList = (List<Course>) courses;
            }).subscribe();

If there is no internet goes into doOnError but throws it further so the app goes down, but code like this:

networkApi.getList()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<List<? extends Course>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    e.getMessage();
                }

                @Override
                public void onNext(List<? extends Course> list) {
                    coursesView.populateRecyclerView(list);
                    courseList = (List<Course>) list;
                }
            });

Work how I expect, it means when there is no internet connection it does nothing.


回答1:


Basically, doOnError does not handle the error, in the sense that it does not consume it. It just does something with it, log it, for example. (The same is true for doOnNext - it also does not consume the item and the item still ends up in onNext of the Subscriber).

The error is still sent down the chain and would still end up in the onError of your Subscriber.

I am pretty certain that your app is crashing with an OnErrorNotImplementedException and that is because you don't have any Subscriber at all and therefore no onError method.



来源:https://stackoverflow.com/questions/33670101/why-rxjava-with-retrofit-on-android-doonerror-does-not-work-but-subscriber-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!