Retrofit 2 Callback onResponse on background thread

Zishan :

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!

iagreen :

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();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=451403&siteId=1