Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private fun loadData() = launch(uiContext) {
view.showLoading() // ui thread
val task = async(bgContext) { dataProvider.loadData("Task") }
// non ui thread, suspend until the task is finished or return null in 2 sec
val result = withTimeoutOrNull(2, TimeUnit.SECONDS) { task.await() }
view.showData(result) // ui thread
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment