Skip to content

Instantly share code, notes, and snippets.

@dmytrodanylyk
Last active October 10, 2017 04:25
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 dmytrodanylyk/fd11bb50cc63d7d90abc7f31f8751bd1 to your computer and use it in GitHub Desktop.
Save dmytrodanylyk/fd11bb50cc63d7d90abc7f31f8751bd1 to your computer and use it in GitHub Desktop.
data class Result<out T>(val success: T? = null, val error: Throwable? = null)
private fun loadData() = launch(uiContext) {
view.showLoading() // ui thread
val task = async(bgContext) { dataProvider.loadData("Task") }
val result: Result<String> = task.await() // non ui thread, suspend until the task is finished
if (result.success != null) {
view.showData(result.success) // ui thread
} else if (result.error != null) {
result.error.printStackTrace()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment