有关何时以及是否将 subscribeOn
与 Retrofit 一起使用的信息存在冲突。
Here is an answer说不要使用 subscribeOn
。
Here is an answer似乎暗示 subscribeOn
没有好的默认设置。
Here is example code使用 subscribeOn
。
那么,一劳永逸,我应该什么时候使用 subscribeOn
以及什么线程?使用或不使用 subscribeOn
可能产生哪些后果?
apiService.issueRequest()
// Is this useful? Required? Bad practice?
.subscribeOn(Schedulers.io())
// Do actions on main thread
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Response>() {
@Override public void call(Response response) {
handleResponse(response);
});
最佳答案
在当前版本的 Retrofit (1.9.0) 中,Retrofit 使用自己的执行器来执行 http 调用并且不使用 the executor backed by the schedulers由 subscribeOn
方法给出。
在您的情况下,调度程序将仅用于执行将您的 http 调用添加到改造使用的执行程序的代码。 (所以有点没用。。。)
但是,关于来自 Retrofit on Github 的实际代码, 改进 stop 以使用他的执行器,因此可以改用 RxJava 调度程序。
关于android - 将 subscribeOn 与 Retrofit 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28462839/