July 2018
Intermediate to advanced
266 pages
7h 11m
English
The current implementation of asyncLoadNews() will wait for each of the requests to end. But since each of them returns a list of headlines, we want to join all their results into a single list containing all of them. For this, we can flat map the content of each deferred:
val headlines = requests.flatMap { it.getCompleted()}
So now we have a headlines variable that contains all the headlines fetched concurrently from the three feeds. At this point, asyncLoadNews() will have two sections. The first one is to fetch and organize the data:
private fun asyncLoadNews() = launch { val requests = mutableListOf<Deferred<List<String>>>() feeds.mapTo(requests) { asyncFetchHeadlines(it, dispatcher) } requests.forEach { it.await ...
Read now
Unlock full access