In an Android app, I am using retrofit's observables. I am running into an issue where the observable is not respecting my observeOn. Here is what I am hoping my code would look like:
@GET("/foo.gz")
public Observable
...
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer("http://fooprovider.com")
.build();
FooApi apiManager = restAdapter.create(FooApi.class);
apiManager.getFoo()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SettingsObserver(this));
Right now I believe I have a few options to get network off the main thread. I can wrap this in a callable which executes on a different thread. I can have my Api return Foo and build my own Observable. Or I can pass in an httpExecutor and callbackExecutor to my restAdapter.
I feel like none of those solutions look as clean as my sample code.
Two things:
subscribeOn. We do that for you using the internal HTTP Executor of the RestAdpater.Observable and hand it back to you. The semantics of what happens is all the RxJava library.We do this many times:
apiManager.getFoo()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(u -> println(u))
and it works perfectly.
Any updates on this? I'm going to preemptively close since I'm quite confident that this is working and it's just a user disconnect or application-level bug.
@JakeWharton is this still true of retrofit 2?
I think i see in RxJavaCallAdapterFactory where we can specify the default scheduler... i assume that's what i should use then.
2 is synchronous by default
On Wed, Feb 24, 2016 at 5:41 PM Brian [email protected] wrote:
I think i see in RxJavaCallAdapterFactory where we can specify the
default scheduler... i assume that's what i should use then.—
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/430#issuecomment-188492714.
Ok, so then i need to call subscribeOn(Schedulers.io()) to make sure the network call happens on the IO thread. And the code in master has a way to set a default scheduler for RxJavaCallAdapterFactory via: RxJavaCallAdapterFactory.create(Schedulers.io())
Yep! You got it.
On Wed, Feb 24, 2016 at 5:43 PM Brian [email protected] wrote:
Ok, so then i need to call subscribeOn(Schedulers.io()) to make sure the
network call happens on the IO thread. And the code in master has a way
to set a default scheduler for RxJavaCallAdapterFactory via:
RxJavaCallAdapterFactory.create(Schedulers.io())—
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/430#issuecomment-188493310.
Just to clarify: is it enough to just set default scheduler in my Retrofit object by calling RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()) (making the call asynchronous) or should I also later call subscribeOn(Schedulers.io()) when subscribing to the created Retrofit Observable?
Do these calls effectively duplicate each other's behavior in this case?
My goal is simply to make an async network call and report the result back to the main thread.
Do these calls effectively duplicate each other's behavior in this case?
yes
Most helpful comment
Two things:
subscribeOn. We do that for you using the internal HTTPExecutorof theRestAdpater.Observableand hand it back to you. The semantics of what happens is all the RxJava library.We do this many times:
and it works perfectly.