If I want to replace livedata with StateFlow, shou...
# coroutines
d
If I want to replace livedata with StateFlow, should I be using
collect
or
onEach
to subscribe to the updates on Stateflow
p
`collect {}`or `onEach {}.collect()`are pretty much the same.
w
or
onEach { }.launchIn(scope)
if I recall correctly
Basically just
onEach
is not starting the collection yet, so
flowOf(…).onEach { println(it) }
won’t do anything. Only when you start collecting with
collect { }
or
launchIn
, the flow will be active
d
Gotcha ty.
z
.launchIn(scope)
and
.collect()
are definitely not the same – the former will launch a new coroutine and return immediately, the latter will suspend until the flow terminates.
👍 6