retrofit icon indicating copy to clipboard operation
retrofit copied to clipboard

Run suspending calls on `Dispatchers.Default`

Open jingibus opened this issue 3 years ago • 1 comments
trafficstars

Run suspending calls within an optional CoroutineDispatcher, or Dispatchers.Default if none is provided.

Our first call to any Retrofit method is blocked by the initialization of our OkHttpClient (a prerequisite for the creation of a Call). On suspend APIs in Android, this is a problem: up until the invocation of call.enqueue, the caller will be running synchronously on the UI thread, which means the UI thread is blocked waiting on OkHttpClient initialization. This has been causing ANRs for us, even after attempting to work around by triggering initialization earlier in the app's lifecycle.

This fixes the issue by running all calls on a CoroutineDispatcher, either one provided in Retrofit.Builder or, failing that, Dispatchers.Default.

Since this changes forces all exceptions off of the calling stack frame and onto a CoroutineDispatcher, the hacks to get around limitations in exception throwing from Java Proxy objects can be safely removed.

jingibus avatar Feb 11 '22 03:02 jingibus

Modified to remove the public API to set the CoroutineDispatcher. This keeps Kotlin out of the ABI. Instead, always use Dispatchers.Default.

jingibus avatar Mar 30 '22 22:03 jingibus