iTranslated by AI
Introduction to StateFlow and Its Caveats
This is a revised and updated version of a Note article.
What is StateFlow?
There is a need to make Flow convenient for representing updatable states in an application.
In this change, StateFlow -- an updatable value representing state, which is also a Flow -- has been introduced. It is designed to flexibly respond to various needs.
The StateFlow<T> interface is a read-only view that provides access to the current value and implements Flow<T> for collecting updates to the value.
The MutableStateFlow<T> interface adds value modification operations.
A constructor function MutableStateFlow(x) is provided.
This returns an implementation of MutableStateFlow with a given initial value.
If non-reactive high-speed access to the value is needed, it can be exposed as StateFlow<T>, and if only reactive representation of updates to the value is needed, it can be exposed as Flow<T>.
The core StateFlow API can be summarized as follows.
Translated with www.DeepL.com/Translator (free version)
It seems you can retrieve values, and with MutableStateFlow, you can also set values.
Now that I fully understand it, let's try it out.
ViewModel
class SubFragmentViewModel(val id: Int, val repository: Repository) : ViewModel() {
val _message = MutableStateFlow<String?>(null)
val message: StateFlow<String?> get() = _message
val flowMessage: Flow<String?> get() = _message
fun setMessage(message: String?) {
_message.value = message
}
fun loadMessage() {
Log.d("AAAAAAAAAAAAAA", message.value)
}
fun loadMessage2() {
viewModelScope.launch {
message.collect {
Log.d("BBBBBBBBBBBBBBB", it)
}
}
}
fun loadMessage3() {
viewModelScope.launch {
flowMessage.collect {
Log.d("CCCCCCCCCCCCCCCC", it)
}
}
}
}
I prepared _message as a MutableStateFlow, message which references it and casts it to StateFlow, and flowMessage which casts it to Flow.
Calling setMessage() sets a string to the _message of MutableStateFlow.
To read the set values, I prepared loadMessage() to getValue from the StateFlow message, one to collect from message and read whenever there's a change, and another to collect values from the Flow flowMessage.
Fragment
class SubFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.setMessage("TEST")
viewModel.setMessage("TEST2")
viewModel.setMessage("TEST3")
viewModel.loadMessage()
viewModel.loadMessage2()
viewModel.loadMessage3()
viewModel.setMessage("TEST4")
viewModel.setMessage("TEST5")
}
}
In the Fragment, after calling setMessage three times to set the strings prepared in the ViewModel, I called loadMessage to read the values and finally decided to check the behavior when setting the strings again.
Operation Results
2020-05-20 07:12:13.503 29370-29370/com.example.masterofkoin D/AAAAAAAAAAAAAA: TEST3
2020-05-20 07:12:13.559 29370-29370/com.example.masterofkoin D/BBBBBBBBBBBBBBB: TEST5
2020-05-20 07:12:13.562 29370-29370/com.example.masterofkoin D/CCCCCCCCCC
As expected, getValue on StateFlow only retrieves the value at the time of the call, but it was unexpected that only the latest value could be retrieved when using collect.
Re-verification (Setting values with delay)
fun pushMessage() {
viewModelScope.launch {
setMessage("TEST")
setMessage("TEST2")
setMessage("TEST3")
delay(500)
setMessage("TEST4")
setMessage("TEST5")
}
}
I inserted a delay during the message setting process.
2020-05-20 08:08:07.574 6563-6563/com.example.masterofkoin D/BBBBBBBBBBBBBBB: TEST3
2020-05-20 08:08:07.575 6563-6563/com.example.masterofkoin D/CCCCCCCCCCCCCCCC: TEST3
2020-05-20 08:08:08.075 6563-6563/com.example.masterofkoin D/BBBBBBBBBBBBBBB: TEST5
2020-05-20 08:08:08.076 6563-6563/com.example.masterofkoin D/CCCCCCCCCCCCCCCC: TEST5
I was successfully able to collect twice for TEST3 and TEST5.
Summary
Care is needed if you must detect every single change without fail.
Discussion