This has probably been asked before but didn't seem to have a viable answer. We are using Retrofit 2.1.0 async callback which carries out the request on the background thread. However, when the response is received onResponse
it is sent back on the application's UI thread regardless of me placing the callback within a new thread forcing it to not utilize the UI.
As we don't want to block the main UI thread for any reason, is it possible for the response to be returned in the background?
If the above is not possible, is it recommended that a separate thread is kicked off from the response in order to avoid UI thread blocking?
Any help would be much appreciated. Thanks!
Retrofit is a third-party library by Square for communicating with REST APIs over HTTP. Similarly to AsyncTasks , work is done in a background thread, and upon completion it returns the result to the main UI thread.
Interface Callback<T>Communicates responses from a server or offline requests. One and only one method will be invoked in response to a given request. Callback methods are executed using the Retrofit callback executor.
2.1. In retrofit, synchronous methods are executed in the main thread. This means that the UI is blocked during the synchronous request execution and no user interaction is possible in this period. In the above example, we have used the ServiceGenerator class.
You can specify a specific executor for the callbacks to run on when building your Retrofit
instance. Here is an example that uses a SingleThreadExecutor
for callbacks.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(/* your url */)
.callbackExecutor(Executors.newSingleThreadExecutor())
// other builder options...
.build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With