How to get Hacker News top stories using parallel coroutine and Retrofit
Issue #379
1 | interface Api { |
1 | class Repo { |
1 | class ViewModel(val repo: Repo): ViewModel() { |
Running parallel
The above run in serial. To run in parallel, we can use async
1 | import kotlinx.coroutines.async |
Parallel decomposition
https://medium.com/@elizarov/structured-concurrency-722d765aa952
With structured concurrency async coroutine builder became an extension on CoroutineScope just like launch did. You cannot simply write async { … } anymore, you have to provide a scope. A proper example of parallel decomposition becomes:
coroutineScope
https://proandroiddev.com/part-2-coroutine-cancellation-and-structured-concurrency-2dbc6583c07d
coroutineScope function can be used to create a custom scope that suspends and only completes when all coroutines launched within that scope complete. If any of the children coroutines within the coroutineScope throws an exception, all other running sibling coroutines gets cancelled and this exception is propagated up the hierarchy. If the parent coroutine at the top of the hierarchy does not handle this error, it will also be cancelled.
awaitAll
Awaits for completion of given deferred values without blocking a thread and resumes normally with the list of values when all deferred computations are complete or resumes with the first thrown exception if any of computations complete exceptionally including cancellation.
This function is not equivalent to deferreds.map { it.await() } which fails only when it sequentially gets to wait for the failing deferred, while this awaitAll fails immediately as soon as any of the deferreds fail.
This suspending function is cancellable. If the Job of the current coroutine is cancelled or completed while this suspending function is waiting, this function immediately resumes with CancellationException.