单个观察者没有调用 onError()

我在 Android 应用程序的 MainActivity 中编写了以下代码。当我运行以下代码时,它不会抛出任何异常并且 onError() 也不会被调用。但是我看到onSuccess: testing starts 两次但我没有看到onSuccess: testing ends。为什么没有调用 onError() 和/或为什么应用程序没有崩溃?


Single.timer(1000, TimeUnit.MILLISECONDS)

                .subscribeOn(Schedulers.computation())

                .subscribeWith(new DisposableSingleObserver<Long>() {

                    @Override

                    public void onSuccess(Long initiationTimeStamp) {

                        String s = null;

                        Log.d(TAG, "onSuccess: testing starts");

                        Log.d(TAG, "onSuccess:test  "+ s.isEmpty());

                        Log.d(TAG, "onSuccess: testing ends");

                    }


                    @Override

                    public void onError(Throwable e) {

                        e.printStackTrace();

                    }

                });


函数式编程
浏览 111回答 2
2回答

www说

您正在调用s.isEmpty()一个NULL字符串,这就是它在第一次打印时结束的原因。也就是说onSuccess()不会抛出任何东西,所以它只是在NullPointerException抛出时停止执行(它在 RxJava 中为你默默地处理)。一旦你订阅 observable,你就会得到 in 的初始值onSuccess(),然后如果它发生变化或者你重新订阅你会得到另一个值 in onSuccess(),这就是它被调用两次的原因。并且因为是针对操作链中发生的错误,所以在抛出异常时onError()不会出错。onSuccess()这种行为是故意的。根据 Rx 合同,观察者不应同时接收onSuccess()和onError()。您需要自己处理异常onSuccess()。例如:Single.timer(1000, TimeUnit.MILLISECONDS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribeOn(Schedulers.computation())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribeWith(new DisposableSingleObserver<Long>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(Long initiationTimeStamp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String s = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "onSuccess: testing starts");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "onSuccess:test&nbsp; "+ s.isEmpty());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "onSuccess: testing ends");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Throwable ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // tell the upstream we can't accept any more data (OPTIONAL)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispose();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // pass error to error handler&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onError(ex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onError(Throwable e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });祝你好运 :)

互换的青春

onError用于沿着操作员链发生的错误。你在里面做的onSuccess已经在结尾了,不会在onError.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java