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

dan.the.man

02/25/2021, 8:16 PM
If I want to replace livedata with StateFlow, should I be using
collect
or
onEach
to subscribe to the updates on Stateflow
p

Panu

02/25/2021, 8:22 PM
`collect {}`or `onEach {}.collect()`are pretty much the same.
w

wasyl

02/25/2021, 8:23 PM
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

dan.the.man

02/25/2021, 8:25 PM
Gotcha ty.
z

Zach Klippenstein (he/him) [MOD]

02/25/2021, 8:58 PM
.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
11 Views