RxJava subscribeOn和observeOn不会覆盖原有的调度程序之前设置? [英] RxJava subscribeOn and observeOn not override the original Scheduler set before?

查看:1075
本文介绍了RxJava subscribeOn和observeOn不会覆盖原有的调度程序之前设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用RxJava在android系统与改造2,我已经调用subscribeOn(Schedulers.io())的Andr​​oid observeOn(AndroidSchedulers.mainThread())的全球认购()之前。
不过,有时我想打电话给subscribeOn(Schedulers.immediate())的Andr​​oid observeOn(Schedulers.immediate())覆盖调度设置之前得到同步的过程。但我发现它不工作是,Android将在IO进行处理还是()线,()由mainThread处理的Andr​​oid的结果。
为什么呢?

I used RxJava in android with Retrofit 2 and I have invoked subscribeOn(Schedulers.io()) android observeOn(AndroidSchedulers.mainThread()) global before subscribe(). However, sometime I would like to call subscribeOn(Schedulers.immediate()) android observeOn(Schedulers.immediate()) to override the Schedulers set before to get synchronized process. But I found it doesn't work, android works would be still processed on io() thread, android result processed by mainThread(). Why?

推荐答案

这只是RxJava的工作方式。

That's just the way RxJava works.

看看这个视频教程,开始于十二时50分大关。因此,考虑在视频中的例子:

Take a look at this video tutorial, starting at the 12:50 mark. So given the example in the video:

Observable.just(1, 2, 3)
    .subscribeOn(Schedulers.newThread())
    .subscribeOn(Schedulers.io())
    .subscribe(System.out::println);

什么情况是, subscribeOn()巢所有通话。在这种情况下 subscribeOn(Schedulers.io())首先催生并订阅一切它上面的IO线程上。但随后 subscribeOn(Schedulers.newThread())旁边催生和它的优先级(因为它被称为最后一个)在其订阅而不是一切。没有建筑物线程链。在这个例子中,你基本上是在产卵IO线程没有很好的理由。

What happens is that subscribeOn() nests all calls. In this case subscribeOn(Schedulers.io()) is spawned first and subscribes everything above it on the io thread. But then subscribeOn(Schedulers.newThread()) is spawned next and it takes priority (since it was called last) to subscribe everything on it instead. There is no building a chain of threads. In this example, you are essentially spawning the io thread for no good reason.

要更好地处理 subscribeOn() observeOn()方法,我建议你先看看< A HREF =htt​​p://blog.danlew.net/2015/03/02/dont-break-the-chain/相对=nofollow>这篇文章从视频的同一作者。他提议是使用变压器来包装调用这些方法:

To better handle the subscribeOn() and observeOn() methods, I suggest you take a look at this post from the same author of the video. What he is proposing is to use a Transformer to wrap the call to these methods:

变压器其实只是 FUNC1&LT;观测&LT; T&GT ;,观测&LT; R&GT;&GT; 。在
  换句话说,给它一个一类的观测,它会返回一个
  观测的另一个。这是完全一样的调用一系列
  运营商内联。

Transformer is actually just Func1<Observable<T>, Observable<R>>. In other words: feed it an Observable of one type and it'll return an Observable of another. That's exactly the same as calling a series of operators inline.

这样的话,你可以有一个方法,像这样:

This way, you can have a method like so:

<T> Transformer<T, T> applySchedulers() {  
    return observable -> observable.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
}

或者,如果你想重用你的变压器,可以有以下设置:

Or, if you want to reuse your transformers, you can have the following setup:

final Transformer schedulersTransformer =  
    observable -> observable.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());

@SuppressWarnings("unchecked")
<T> Transformer<T, T> applySchedulers() {  
    return (Transformer<T, T>) schedulersTransformer;
}

接着上面的例子如下:

Then the above example would look like:

Observable.just(1, 2, 3)
    .compose(applySchedulers())
    .subscribe(System.out::println);

希望有所帮助。

这篇关于RxJava subscribeOn和observeOn不会覆盖原有的调度程序之前设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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