Summary
This chapter covered many interesting topics of practical concurrency in Kotlin. Let's do a recap of the key topics.
- Android applications will throw
NetworkOnMainThreadException
if a network request is done on the UI thread. - Android applications can only update the UI on the UI thread, and trying to do it from a different thread will produce a
CalledFromWrongThreadException
. - Network requests have to be done in a background thread. The information has to be sent to the UI thread for the views to be updated.
- A
CoroutineDispatcher
can be used to enforce a coroutine to run in a specific thread, or group of threads. - One or many coroutines can be run in a thread by using
launch
orasync
. launch
should be used in fire-and-forget scenarios, meaning cases where we aren't expecting the coroutine to return something.async
should be used when the coroutine will produce a result that will be processed. Usingasync
without handling its result can result in exceptions not being propagated.- Kotlin has specific...