Member-only story

Make sure to update your StateFlow safely in Kotlin!

Patryk Kosieradzki
ProAndroidDev
Published in
3 min readJul 25, 2021
Photo by Matt Hardy on Unsplash

Hi, today I come to you with a quick tip on how to update your StateFlows safely in Kotlin.

Recently a new version of Kotlin Coroutines library was released with a few new extensions functions to help you with StateFlow updates. It all started with this issue:

Let’s see what it’s all about…

One last thing before we start

✉️ Android Dev Newsletter

If you enjoy learning about Android like I do and want to stay up to date with the latest, worth reading articles, programming news and much more, consider subscribing to my newsletter 👇
https://androiddevnews.com/

🎙 Android Talks Podcast

If you’re a Polish speaker and want to listen to what I have to say about Android, architecture, security and other interesting topics, check out my podcast 👇
https://androidtalks.buzzsprout.com/

First time using StateFlow?

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Patryk Kosieradzki

Senior Android &iOS Engineer @ Moss, based in Warsaw, Poland

Responses (3)

What are your thoughts?

Looks like great minds think alike! :)
I wrote a very similar article a few days ago! I'm glad to see I'm not the only one who thinks this is worth telling others about.

Why not using kotlin Mutex to update state? Mutex grants lock and synchronization. https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html#mutual-exclusion

Do we have this same problem if use LiveData instead of StateFlow?