如何处理主线程中的RxAndroid错误 [英] how to handle RxAndroid errors in the main thread

查看:250
本文介绍了如何处理主线程中的RxAndroid错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是rxJava / Android的新手,但感到惊讶的是,有时我会在主线程上调用我的 onError lambda,有时却没有,尽管我使用了 .observeOn(AndroidSchedulers.mainThread())

I'm new to rxJava/Android and surprised that my onError lambda is sometimes called on the main-thread and sometimes not, although I use .observeOn(AndroidSchedulers.mainThread())

示例1 onError 在主线程上

可以正常工作: onError 在主线程上被调用

Example 1: onError on main-thread
this works as expected: onError is called on the main-thread

Observable.error(new RuntimeException("RTE"))
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(s -> {
                Log.e(TAG, "onNext("+s+")-thread: " + Thread.currentThread().getName());
            },
            throwable -> {
                Log.e(TAG, "onError()-thread: " + Thread.currentThread().getName());
            });

日志输出:

onError()-thread: main

示例2 onError 在主线程上

Observable.create(new Observable.OnSubscribe<String>() {
    @Override
    public void call(Subscriber<? super String> subscriber) {
        subscriber.onNext("one and only");
    }
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.timeout(1, TimeUnit.SECONDS)
.subscribe(s -> {
        Log.e(TAG, "onNext("+s+")-thread: " + Thread.currentThread().getName());
    },
    throwable -> {
        Log.e(TAG, "onError()-thread: " + Thread.currentThread().getName());
    });

输出是这样的:

onNext(one and only)-thread: main
onError()-thread: RxComputationScheduler-4       

我认为在调用 observeOn(AndroidSchedulers.mainThread()),所有发射都应在主线程上完成。

I thought that after calling observeOn(AndroidSchedulers.mainThread()), ALL emissions should be done on the main-thread.

所以我有以下问题:


  1. 我找不到任何指定在哪种情况下在哪个线程中调用 onError 的文档。有人知道链接吗?

  2. 我当然想在GUI中显示一些错误指示:所以我该如何强制 onError 在主线程中总是被调用?

  1. I could not find any documentation that specifies under what circumstances onError is called in which thread. Anyone knows a link?
  2. I do of course want to display some error-indication in the GUI: so how can I force onError to be ALWAYS called in the main-thread?


推荐答案

我只是发现我只需要更改呼叫顺序。当我在超时 observeOn 时c $ c>可以按预期工作:

I just found out that I only need to change the order of calls. When I call observeOn after timeout it works as expected:

.timeout(1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())

日志输出

onNext(one and only)-thread: main
onError()-thread: main

原因是, observeOn 仅会影响调用下方的所有内容,并且仅在其他运算符再次更改线程之前才会生效。在上面的示例中, timeout()将更改为计算线程。

The reason is, that observeOn will only affect everything below the call, and only until some other operator changes the thread again. In the example above, timeout() will change to the computation thread.

请注意, subscribeOn 的工作方式有所不同。

您应该只调用一次(当多次调用它时,第一次调用获胜:请参见中的 Multiple subscriptionOn 这个博客

Note, that subscribeOn works differently. It does not matter where in the chain you call it.
You should only call it once (when you call it multiple times the first call wins: see "Multiple subscribeOn" in this Blog)

这里是一个不错的博客文章,其中包含更多详细信息: RxJava-了解observeOn ()和subscribeOn()

Here is a nice blog-post with more details: RxJava- Understanding observeOn() and subscribeOn()

这篇关于如何处理主线程中的RxAndroid错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆