Kotlin Flows ~ an Android cheat sheet

TLDR: Cheat sheet below. Download PDF version for clickable links.

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.

Kotlin Flow
Recently the coroutines API has expanded a lot with the addition of different kind of Flows. There are already excellent in dept 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 😊

My thanks goes out to Manuel Vivo and Louis CAD for their reviews and input.

Download the PDF version here




Android developer @ Egeniq.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Exploring Collections and Sequences in Kotlin

Installing an Android App Bundle (.aab) app on an Android Device

Implementing Ads in Your Android Application

?How Did I Publish My Very First Android App

Working with Firebase Storage in Android: Part 1

A Significant Improvement Made Google Pixel 6 & 6 Pro Fingerprint Scanner With The Latest Android…

A Simple way to work with Excel in Android App

A New Way to Handle Back Press in Fragments

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Remy Benza

Remy Benza

Android developer @ Egeniq.com

More from Medium

How to inject the coroutines Dispatchers into your testable code

Ktor-Client Decoded!! What, Why, How and When????

Out with Retrofit and in with KTor Client

How to Manually Retry a Call Using Kotlin Flow?