Last active
March 25, 2021 08:33
-
-
Save diewland/37b2904d554864e38e38e5f20d1b4e66 to your computer and use it in GitHub Desktop.
Loop heavy blocking process without UI freeze
This file contains 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
fun main() { | |
val h = Handler(Looper.getMainLooper()) | |
GlobalScope.launch(Dispatchers.Main) { | |
(1..5).forEach { proc(it, h) } | |
} | |
h.removeCallbacksAndMessages(null) | |
} | |
suspend fun proc(no: Int, h: Handler): Boolean = suspendCoroutine { | |
h.post { | |
Thread.sleep(1_000) // <-- heavy blocking process | |
tvStatus.text = "process #$num" | |
it.resume(true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment