将 subscribeOn 与 Retrofit 结合使用
Using subscribeOn with Retrofit
有关何时以及是否将 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使用自己的executor来执行http调用,并没有使用subscribeOn
方法给出的the executor backed by the schedulers。
在您的情况下,调度程序将仅用于执行将您的 http 调用添加到改造使用的执行程序的代码。 (所以有点没用。。。)
BUT,关于 Retrofit on Github 的实际代码,改造停止使用他的执行器,因此可以使用 RxJava 调度程序代替。
有关何时以及是否将 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使用自己的executor来执行http调用,并没有使用subscribeOn
方法给出的the executor backed by the schedulers。
在您的情况下,调度程序将仅用于执行将您的 http 调用添加到改造使用的执行程序的代码。 (所以有点没用。。。)
BUT,关于 Retrofit on Github 的实际代码,改造停止使用他的执行器,因此可以使用 RxJava 调度程序代替。