Retrofit 2 Callback onResponse in background thread

This was probably asked earlier, but there did not seem to be a viable answer. We use the Retrofit 2.1.0 asynchronous callback, which executes the request in the background thread. However, when the response is received by onResponse , it is sent back to the application user interface thread, regardless of the fact that I put the callback in the new thread, causing it not to use the user interface.

Since we don’t want to block the main UI thread for any reason, is it possible for the response to return in the background?

If this is not possible above, is it recommended to disconnect a separate thread from the response in order to avoid blocking user interface threads?

Any help would be greatly appreciated. Thanks!

+7
java android multithreading retrofit2
source share
1 answer

You can specify a specific artist to perform callbacks when creating a Retrofit instance. Here is an example that uses the SingleThreadExecutor for callbacks.

 Retrofit retrofit = new Retrofit.Builder() .baseUrl(/* your url */) .callbackExecutor(Executors.newSingleThreadExecutor()) // other builder options... .build(); 
+12
source share

All Articles