It just occured to me that `MutableStateFlow` can ...
# coroutines
l
It just occured to me that
MutableStateFlow
can be used for databinding. Did anyone try that yet?
w
Do you mean it’s automatically unwrapped just like
LiveData
?
l
I meant
MutableStateFlow
(edited). @wasyl I mean you can hand it to UI code, and it will get the current value, the updates, and be able to push a new value.
s
Yup, we used it in one of our projects
Ah.. sorry, we have used it for our Activities/Fragment, but not through the DataBinding framework (we used just plain code)
l
When I say databinding, I'm not referring to any framework. It works on Android indeed, but could also work on Kotlin/JS, and on Kotlin/Native single-thread.
s
Then, yes! 🙂
On Android, the collector (Fragment/Activity) uses a scope with a PausingDispatcher to mimic the active/in-active behavior of the LiveData.
We use our own version of an
EventFlow
and
StateFlow
on a prototype KMP project (targeting Android and iOS).
👌 2
r
KVision allows using
StateFlow
for databinding with observable components. But I haven't used this functionality in any real project yet.
👍 1
a
@louiscad That sounds interesting, do you have some sort of a gist? or repo that I can check out? I'd really like to see how you guys implemented that
l
@andylamax I have no gist at the moment, but the idea is to basically pass a
MutableStateFlow
to the part of the code that will manage the UI, or a part of it. I pass it as part of a cancellable suspending function (for when that UI part needs to be disabled), and in that function, I collect the
StateFlow
with
collectLatest
or alike, update the UI, and wait for user interaction to input a new value that is then put into the same
MutableStateFlow
.
z
We're using it to emit (non-jetpack) view models from our state management library, Workflow