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

timrijckaert

01/10/2021, 4:51 PM
Does anybody know how you can create a
MutableStateFlow
that allows duplicate events to be send. Basically mitigate the default
distinctUntilChanged
behavior?
b

bezrukov

01/10/2021, 4:56 PM
Use SharedFlow instead, there is no way to do this with stateflow idiomatically (there is an ugly way though - via object wrapper, but even in this case some items may be lost if collector is slower than producer)
1
t

timrijckaert

01/10/2021, 5:00 PM
Can you give a simple sample of the
SharedFlow
that allows duplicate events?
b

bezrukov

01/10/2021, 5:16 PM
Yep, something like
Copy code
MutableSharedFlow<Event>(1, extraBufferCapacity = 64)
👍 1
u

ursus

01/10/2021, 8:41 PM
wait what? if I stateFlow.value = "5" twice, it will get emitted only once?
b

bezrukov

01/10/2021, 8:45 PM
yes, because it represents "state" (obviously from its name) when you set it to "5" second time, state was not changed
☝️ 1
u

ursus

01/10/2021, 9:10 PM
debatable, but okay
+ equals can be heavy ..a waste for collections, etc near UI especially, which will get diffed anyways
4 Views