Skip to content

Instantly share code, notes, and snippets.

@diewland
Last active March 25, 2021 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diewland/37b2904d554864e38e38e5f20d1b4e66 to your computer and use it in GitHub Desktop.
Save diewland/37b2904d554864e38e38e5f20d1b4e66 to your computer and use it in GitHub Desktop.
Loop heavy blocking process without UI freeze
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