Run suspending calls on Dispatchers.Default
#3693
Open
+84
−48
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Run suspending calls within an optional
CoroutineDispatcher
, orDispatchers.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 aCall
). Onsuspend
APIs in Android, this is a problem: up until the invocation ofcall.enqueue
, the caller will be running synchronously on the UI thread, which means the UI thread is blocked waiting onOkHttpClient
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 inRetrofit.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.