- Learning Concurrency in Kotlin
- Miguel Angel Castiblanco Torres
- 73字
- 2021-08-05 10:46:49
Using Android's UI coroutine dispatcher
With this done, we can now use the dispatcher in the same way we use any other, for example:
launch(dispatcher) {
val headlines = fetchRssHeadlines()
val newsCount = findViewById<TextView>(R.id.newsCount)
launch(UI) {
newsCount.text = "Found ${headlines.size} News"
}
}
The UI dispatcher comes from the library we just added, kotlinx-coroutines-android.
Executing the code at this point will work. The quantity of news will be correctly displayed on the application.