July 2018
Intermediate to advanced
266 pages
7h 11m
English
This brings us to the second option. We can write a function, asyncLoadNews(), that will contain the launch() inside it and return the resulting Job. This function can be called without a launch() block regardless of the thread; and by returning the Job, it's possible for the caller to cancel it:
private fun asyncLoadNews() = launch(dispatcher) { val headlines = fetchRssHeadlines() val newsCount = findViewById<TextView>(R.id.newsCount) launch(UI) { newsCount.text = "Found ${headlines.size} News" }}
Read now
Unlock full access