本文介绍了为什么在Android doOnError()上进行翻新的RxJava不起作用,而Subscriber onError起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么这样的代码:

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

如果没有互联网进入doOnError但将其进一步抛出,则应用程序将关闭,但代码如下:

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.

推荐答案

基本上,doOnError不会处理错误,因为它不会消耗它.例如,它只是执行某些操作,将其记录下来. (doOnNext也是如此-它也不会消耗该物料,并且物料仍以SubscriberonNext结尾.)

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).

错误仍然沿着链发送,并且最终会出现在SubscriberonError中.

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

我非常确定您的应用程序崩溃时出现了OnErrorNotImplementedException,这是因为您根本没有任何Subscriber,因此也没有onError方法.

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.

这篇关于为什么在Android doOnError()上进行翻新的RxJava不起作用,而Subscriber onError起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:22