Run suspending calls on Dispatchers.Default#3693
Closed
jingibus wants to merge 1 commit intosquare:trunkfrom
Closed
Run suspending calls on Dispatchers.Default#3693jingibus wants to merge 1 commit intosquare:trunkfrom
Dispatchers.Default#3693jingibus wants to merge 1 commit intosquare:trunkfrom
Conversation
cb34de1 to
4bb9250
Compare
4bb9250 to
98ad945
Compare
Author
|
Modified to remove the public API to set the |
98ad945 to
b0770ff
Compare
Dispatchers.Default
b0770ff to
632bc53
Compare
This addresses a need for off-main-thread invocation of Call.Factory.newCall to support lazy HttpClient initialization.
632bc53 to
84350cc
Compare
Goooler
reviewed
Jan 2, 2024
| @@ -30,57 +30,63 @@ import kotlin.coroutines.resumeWithException | |||
| inline fun <reified T: Any> Retrofit.create(): T = create(T::class.java) | |||
|
|
|||
| suspend fun <T : Any> Call<T>.await(): T { | |||
Contributor
There was a problem hiding this comment.
Use these would be less git changes, and needs to rebase.
suspend fun <T : Any> Call<T>.await(): T = withContext(Dispatchers.Default) {suspend fun <T : Any> Call<T?>.await(): T? = withContext(Dispatchers.Default) {suspend fun <T> Call<T>.awaitResponse(): Response<T> = withContext(Dispatchers.Default) {
Contributor
|
How about to instead of hardcoding a dispatcher, allow devs to provide a dispatcher.. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.Defaultif 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). OnsuspendAPIs 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 onOkHttpClientinitialization. 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.Builderor, 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.