Make DispatchQueue drain the queue immediatly from maybeEnqueueConsumer if run on the main thread
Currently things launched with `launchWhenCreated` will run after
`onStart` since `Dispatchers.Main` is used. With this change
maybeEnqueueConsumer will drain the queue synchronously if run on the
main thread which prevents this behavior.
Test: ./gradlew lifecycle:lifecycle-ktx:test
Change-Id: I0f8c80b438a95257ae64ad545ef76c00cf438c41
diff --git a/lifecycle/lifecycle-runtime-ktx/src/main/java/androidx/lifecycle/DispatchQueue.kt b/lifecycle/lifecycle-runtime-ktx/src/main/java/androidx/lifecycle/DispatchQueue.kt
index 7ace839..e46fe24 100644
--- a/lifecycle/lifecycle-runtime-ktx/src/main/java/androidx/lifecycle/DispatchQueue.kt
+++ b/lifecycle/lifecycle-runtime-ktx/src/main/java/androidx/lifecycle/DispatchQueue.kt
@@ -79,7 +79,7 @@
@MainThread
fun maybeEnqueueConsumer() {
if (queue.isNotEmpty()) {
- Dispatchers.Main.dispatch(EmptyCoroutineContext, consumer)
+ Dispatchers.Main.immediate.dispatch(EmptyCoroutineContext, consumer)
}
}