https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

louiscad

06/22/2020, 1:26 PM
It just occured to me that
MutableStateFlow
can be used for databinding. Did anyone try that yet?
w

wasyl

06/22/2020, 1:33 PM
Do you mean it’s automatically unwrapped just like
LiveData
?
l

louiscad

06/22/2020, 1:34 PM
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

streetsofboston

06/22/2020, 1:35 PM
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

louiscad

06/22/2020, 1:38 PM
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

streetsofboston

06/22/2020, 1:38 PM
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

Robert Jaros

06/22/2020, 1:48 PM
KVision allows using
StateFlow
for databinding with observable components. But I haven't used this functionality in any real project yet.
👍 1
a

andylamax

06/22/2020, 2:01 PM
@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

louiscad

06/22/2020, 2:06 PM
@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

Zach Klippenstein (he/him) [MOD]

06/22/2020, 2:20 PM
We're using it to emit (non-jetpack) view models from our state management library, Workflow
3 Views