Tobias
07/01/2024, 8:26 AMval monitorDongleEuiJob = settings.dongleEui.takeWhile { dongleEui ->
dongleEui.isNotEmpty()
}.onEach { dongleEui ->
// do something, but nothing happens
}.catch {
print(it.message)
}.launchIn(coroutineScope)
When I change the code to this, it always works, but I don’t undestand why
val monitorDongleEuiJob = settings.dongleEui.onEach { dongleEui ->
if (dongleEui.isEmpty()) {
return@onEach
}
// do something
}.catch {
print(it.message)
}.launchIn(coroutineScope)
Shouldn’t it basically be the same?
Or am I misunderstanding takeWhile
? Maybe does it stop collection when the condition has been met?Sam
07/01/2024, 8:36 AMreturn@onEach
won't terminate the flow, it's just a no-op.Tobias
07/01/2024, 8:37 AMtakeWhile
terminates it? Then I have found my misunderstanding.Sam
07/01/2024, 8:38 AMtakeWhile
is designed to end the flow once its value changes from true
to false
. Maybe you're looking for filter
instead?Tobias
07/01/2024, 8:39 AMval monitorDongleEuiJob = settings.dongleEui
.filter { it.isNotEmpty() }
.onEach { dongleEui ->
...
I think this is what I actually need.Tobias
07/01/2024, 8:39 AM