Skip to content

Instantly share code, notes, and snippets.

@dmytrodanylyk
Last active October 10, 2017 09:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmytrodanylyk/1a5b9d2d0e39677233d8e52c7871a2c2 to your computer and use it in GitHub Desktop.
Save dmytrodanylyk/1a5b9d2d0e39677233d8e52c7871a2c2 to your computer and use it in GitHub Desktop.
private fun loadData() = launch(uiContext) {
view.showLoading() // ui thread
// non ui thread, suspend until task is finished
val result1 = async(bgContext) { dataProvider.loadData("Task 1") }.await()
// non ui thread, suspend until task is finished
val result2 = async(bgContext) { dataProvider.loadData("Task 2") }.await()
val result = "$result1 $result2" // ui thread
view.showData(result) // ui thread
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment