Lukasz Kalnik
04/03/2023, 2:56 PMStateFlow
from within the collect
function? Let's say I received the value I expected and want to stop collecting.kevin.cianfarini
04/03/2023, 2:58 PMstateFlow.first { it == theValue }
Lukasz Kalnik
04/03/2023, 3:00 PMLukasz Kalnik
04/03/2023, 3:00 PMfirst
just suspends until the condition is true and I can call my action afterwards?Lukasz Kalnik
04/03/2023, 3:01 PMcoroutineScope.launch {
stateFlow.first { it }
action()
}
Lukasz Kalnik
04/03/2023, 3:01 PMstateFlow
to become true
Lukasz Kalnik
04/03/2023, 3:01 PMkevin.cianfarini
04/03/2023, 3:03 PMCLOVIS
04/03/2023, 3:03 PMstateFlow
.filter { it == theValue }
.onEach { yourAction() }
.first()
Your call on which is more readablekevin.cianfarini
04/03/2023, 3:04 PMonEach
because it’s inherently side effecty but either worksCLOVIS
04/03/2023, 3:04 PMI want to run an action when I get the valueWell that's the definition of a side effect, isn't it?
Lukasz Kalnik
04/03/2023, 3:06 PMfirst
is more concise.jw
04/03/2023, 3:15 PMjw
04/03/2023, 3:16 PMephemient
04/03/2023, 4:23 PMtakeWhile
or transformWhile
, but if you're only interested that it exists at all, then first
followed by non-Flow
code is simplest