Reputation: 10993
The Retrofit Documentation states the following:
For desktop applications callbacks will happen on the same thread that executed the HTTP request.
I've attempted to understand this by studying the Retrofit source (particularly RestAdapter.java
) and looking at a number of other similar SO questions (such as this) but I am still puzzled.
If I have a background thread which calls void getUserPhoto(@Path("id") int id, Callback<Photo> cb)
(for example), exactly how does this library execute a callback on that thread?
Upvotes: 2
Views: 2117
Reputation: 50588
If you pass same executor to the RestAdapter
, let's say:
ExecutorService backgroundExecutor = Executors.newCachedThreadPool();
restAdapterBuilder.setExecutors(backgroundExecutor, backgroundExecutor);
Then the callback Runnable
will be executed on this background executor, which means you will receive callback in same thread, otherwise the thread will be shutdown or reused and the callback will happen on thread different from the one that executed the http request.
When Retrofit
checks if you are running it on desktop app or Android App, it checks if there is a present package from android sdk, and then obtains the executor(s) for http request and callbacks will happen, i.e if you are running on Android, it will run the callback Runnable
on main thread. If you are running on non-Android it will get the synchronous executor (the same that is used for http request) unless you don't pass something else. These are the default Platform settings that are built, if you don't do it yourself.
Upvotes: 4