Blog Infos
Author
Published
Topics
Author
Published
Topics
Posted by: Patryk Kosieradzki

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:

Expose atomic updates on MutableStateFlow · Issue #2720 · Kotlin/kotlinx.coroutines

qwwdfsad added a commit that referenced this issue May 25, 2021 …ableStateFlow (#2729) * Add update, updateAndGet…

github.com

 

Let’s see what it’s all about…

First time using StateFlow?

If it’s your first time with StateFlow and you want to know more about it and how it relates to others like SharedFlow or LiveData, you can read my article about it here:

LiveData vs SharedFlow and StateFlow in MVVM and MVI Architecture

See the original article on my website…

proandroiddev.com

 

Let’s begin

StateFlow is mostly used for handling state and state updates. It can be used for example in Android’s ViewModel to expose state to your views.

Let’s see an example:

class ExampleViewModel(
private val initialState: ExampleViewState
) : ViewModel() {
private val _uiState: MutableStateFlow<ExampleViewState> = MutableStateFlow(initialState)
val uiState = _uiState.asStateFlow()
}
data class ExampleViewState(
val title: String = "",
val description: String = ""
// other state variables
)

How to collect this state now in Fragment for example? All info can be found in my article I mentioned before 🙂

How can we update the state?

Well, it’s really easy, since we use a data class. We can just use copy like this:

_uiState.value = _uiState.value.copy(title = "Something")

Simple, right? But you have to be careful…

The issue

So what’s the problem? While the code is very simple, there is something you have to be aware of: CONCURRENCY.

If between the time copy function completes and the StateFlow’s new value is emitted another thread tries to update the StateFlow — by using copy and updating one of the properties that the current copy isn’t modifying —we could end up with results we were not expecting.

Job Offers

Job Offers


    Senior Android Developer (Remote)

    Komoot
    Europe
    • Full Time
    apply now

    Android Build Engineer

    Pinterest
    San Francisco, CA | Seattle, WA
    • Full Time
    apply now

    Android Developer

    Small and Modern GmbH
    Hamburg, Remote (Germany)
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

,

Keep Rules in the Age of Kotlin

ProGuard has been the industry standard for obfuscating, shrinking and optimizing Java & Android apps for close to 20 years. ProGuard, and the compatible R8 shrinker, usually need some configuration since it’s not always technically…
Watch Video

Jobs

Solution

How do we solve this problem? By using fresh good stuff from Kotlin Coroutines for MutableStateFlow.

The stuff I’m talking about are these three methods:

All of these take a function parameter that returns the new state that will be emitted.

Let’s see what’s inside the update method for a moment:

public inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
    while (true) {
        val prevValue = value
        val nextValue = function(prevValue)
        if (compareAndSet(prevValue, nextValue)) {
            return
        }
    }
}

As we can see, a function is passed as a param and it’s applied to the current StateFlow’s value. Then compareAndSet function is used to determine if the value has changed — for example by another thread. If compareAndSet returns false then the while loop will be running until it’s possible to update the state.

So how to use it? Let’s see an example SAFE update now:

_uiState.update { it.copy(title = "Something") }

That’s it! This ensures us that the state can be changed safely!

That’s all for this article. I hope you’ll update your StateFlow safely now. Be sure to check out my other articles about Android. All links are available here:

Patryk Kosieradzki | Linktree

Linktree. Make your link do more.

linktr.ee

Have a nice day! 🙂

 
Tags: Android,Kotlin, Android App Development, AndroidDev, Android Apps

View original article at:


Originally published: July 25, 2021

 

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE
blog
Modern mobile applications are already quite serious enterprise projects that are developed by hundreds…
READ MORE

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu