Humans are notoriously bad multitaskers. Even though we convinced ourselves we’re not. Science shows as soon as we start to do multiple things at the same time, our concentration drops, we get sloppy and errors ramp up. Computers on the other hand are excellent multitaskers. Every modern computer can do many, many things at the same time, switching effortlessly between tasks, without ever dropping the ball.
Software that enables this concurrent computing is off course written by humans. Nevertheless, writing correct scalable software that supports concurrency is hard. This is why there are a lot of libraries to help developers with this task. Since 2005 when the first consumer multicore CPU was introduced, the number of cores in CPU’s are steadily increasing. So if we want to keep leveraging all this computing power, this challenge becomes increasingly important.
Android lives in the JVM world and mostly uses Threads for concurrency. For years the default library to use on Android for concurrent programming was RxJava. It provided an abstraction over Threads and made switching between them easy. But it came with a very steep learning curve and was quite a heavy library to include in your app. But there is a new kid on the block; Kotlin coroutines. Since it’s inception in 2017 it has gained rapid traction. In 2019 Google made it the official recommended library to use when writing concurrent and/or asynchronous code on Android. As a result many new AndroidX libraries use coroutines and flow.
Recently the coroutines API has expanded a lot with the addition of different kind of Flows. There are already excellent in depth articles and documentation about coroutines (see; one, two, three). My article will merely provide a cheat sheet on the Flow API in coroutines for use on Android. What kind of Flows are there, their differences, what are the RxJava equivalents and where can you find a quick snippet to get you started. Happy coding!