默认调度Android上rxjava [英] Default Schedulers for rxjava on Android

查看:174
本文介绍了默认调度Android上rxjava的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是改造返回rxjava观测对我的异步网络调用。

I'm using Retrofit to return rxjava Observable's for my async network calls.

我发现自己重复下面的调用:

I find myself repeating the following invocation:

<$c$c>someApiCall().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

好像我一直订阅的IO线程和观察的Andr​​oid主线程上。这似乎是所有的资源,我发现提倡的最佳实践。也许比长期运行的计算等,我不明白,当我们希望从这个模式偏离。

Seems like I'm always subscribing on the IO thread and observing on the Android main thread. This seems to be the best practice that all the resources I found advocate. Perhaps other than long-running computations, I don't quite understand when we would want to deviate from this pattern.

有没有办法通过拖欠subscribeOn和observeOn线程删除此样板?

Is there a way to remove this boilerplate by defaulting the subscribeOn and observeOn threads?

这是一个用例 rxjava插件? (我无法找到其使用的例子很多。)

Is this a use case for rxjava plugins? (I can't find many examples of their use.)

我可以通过与<一个搞乱设置在网络边界的默认线程href=\"http://square.github.io/retrofit/javadoc/retrofit/RestAdapter.Builder.html#setExecutors-java.util.concurrent.Executor-java.util.concurrent.Executor-\"相对=nofollow>改造执行人?

推荐答案

有关观测的响应,改造目前设置了 subscribeOn RestAdapter 的HTTP执行人(已提供或默认值)。这样做是为了垫片RxJava支持到现有的行为。

For Observable responses, Retrofit currently sets the subscribeOn as the HTTP executor of the RestAdapter (either provided or the default). This was done to shim RxJava support into the existing behavior.

2.0的计划是提供设置默认值的能力,既 subscribeOn observeOn 明确(不管它既,只有一个,或两者都不是)。

The plan for 2.0 is to provide the ability to set defaults for both subscribeOn and observeOn explicitly (whether it be both, only one, or neither).

你不会想一个原因总想在主线程上的观察是,如果你需要把多个API调用在一起,例如

A reason you wouldn't want always want observation on the main thread is if you needed to chain multiple API calls together, for example.

这篇关于默认调度Android上rxjava的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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