Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task<T>.await() schedules its continuation on the main thread #2990

Closed
petrcermak opened this issue Oct 15, 2021 · 0 comments
Closed

Task<T>.await() schedules its continuation on the main thread #2990

petrcermak opened this issue Oct 15, 2021 · 0 comments

Comments

@petrcermak
Copy link

The Task<T>.await() extension function schedules its continuation on the main thread, which adds unnecessary latency:

addOnCompleteListener {
    val e = it.exception
    if (e == null) {
        @Suppress("UNCHECKED_CAST")
        if (it.isCanceled) deferred.cancel() else deferred.complete(it.result as T)
    } else {
        deferred.completeExceptionally(e)
    }
}

It would be better to avoid this by explicitly passing the direct executor to addOnCompleteListener instead:

addOnCompleteListener(directExecutor()) {
    val e = it.exception
    if (e == null) {
        @Suppress("UNCHECKED_CAST")
        if (it.isCanceled) deferred.cancel() else deferred.complete(it.result as T)
    } else {
        deferred.completeExceptionally(e)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants