Skip to content

Send async messages to the Android main looper #577

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

Closed
wants to merge 1 commit into from
Closed

Send async messages to the Android main looper #577

wants to merge 1 commit into from

Conversation

JakeWharton
Copy link
Contributor

Closes #427

Copy link
Contributor

@LouisCAD LouisCAD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, although I wonder why you didn't raise the compileSdk to 28 to avoid reflection and expose new APIs.

@JakeWharton
Copy link
Contributor Author

It's a jar, not an aar

@LouisCAD
Copy link
Contributor

Is this a problem regarding the SDK used to compile it?

@JakeWharton
Copy link
Contributor Author

Yes as it does not use AGP

@@ -40,7 +42,32 @@ public fun Handler.asCoroutineDispatcher(): HandlerDispatcher =

private const val MAX_DELAY = Long.MAX_VALUE / 2 // cannot delay for too long on Android

private val mainHandler = Handler(Looper.getMainLooper())
private val mainHandler = Looper.getMainLooper().asHandler(async = true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we provide system property to opt-out this behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... maybe. It's a challenging situation because disabling this makes the entire app slower where you might just want to change the behavior for a single coroutine.

It may be worth discussing providing a dispatcher that is always async=false to the main thread so that decisions can be made on a coroutine-by-coroutine basis rather than globally to the whole app.

I worry about the case when someone has a problem, disables async to "fix" it, and then that starts getting cargo culted around slowing down everyone's apps.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.
Let's wait for the feedback from the users and will discuss solutions for their particular problems.

This PR carries enough value to release it as is.

@qwwdfsad qwwdfsad self-assigned this Sep 19, 2018
@qwwdfsad
Copy link
Collaborator

Rebased onto develop.
Thank you!

@qwwdfsad qwwdfsad closed this Sep 19, 2018
@JakeWharton JakeWharton deleted the jakew/async/2018-09-17 branch September 19, 2018 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants