Summary
This chapter focused on doing background operations with Coroutines and Flow. Background operations are used for long-running tasks such as accessing data from the local database or a remote server.
You started with the basics of using Kotlin coroutines, Google’s recommended solution for asynchronous programming. You learned that you can make a background task into a suspending function with the suspend
keyword. Coroutines can be started with the async
or launch
keywords.
You learned how to create suspending functions and how to start coroutines. You also used dispatchers to change the thread where a coroutine runs. Then, you used coroutines for performing network calls and modified the data retrieved with the map
and switchMap
LiveData
transformation functions.
You then moved on to using Kotlin Flow in an Android app to load the data in the background. To safely collect flows in the UI layer, prevent memory leaks, and avoid wasting resources, you can use Lifecycle...