# Make sure to update your StateFlow safely in Kotlin!

DevFeed: [Make sure to update your StateFlow safely in Kotlin!](<https://devfeed.tech/articles/make-sure-to-update-your-stateflow-safely-in-kotlin-25522.md>)

Original publisher: [Read original article](<https://patrykkosieradzki.com/make-sure-to-update-your-stateflow-safely-in-kotlin>)

Published: 2021-07-25T00:00:00Z

Content type: tutorial

Language: en

Sources: [Patryk Kosieradzki](<https://devfeed.tech/sources/patryk-kosieradzki.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [kotlin-coroutines](<https://devfeed.tech/topics/kotlin-coroutines.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [Android](<https://devfeed.tech/topics/android.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [code](<https://devfeed.tech/tags/code.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [data-class](<https://devfeed.tech/tags/data-class.md>), [flow](<https://devfeed.tech/tags/flow.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

This Kotlin tutorial explains how concurrent updates to MutableStateFlow can lose changes when state is copied and emitted manually. It presents the Kotlin Coroutines update, updateAndGet, and getAndUpdate methods, which retry updates using compareAndSet to safely handle concurrent changes.

## Source excerpt

StateFlow is a common choice for storing app state in Android, e.g., view state. But do you know how to use it correctly?