First photo, this is how I usually do stuff Secon...
# coroutines
c
First photo, this is how I usually do stuff Second photo, I didn't know I could do this. I am confused why use onEach though. I thought all of the "on*" operators were side-effecty Edit: Just read the above question. My question is sorta similar it seems. lol
k
It depends on your use case, but I always prefer manually launching versus using
launchIn
.
Copy code
launch {
  flow.collect { item -> ... }
}
1
j
> I am confused why use onEach though •
flow.onEach { doStuff(it) }.collect()
is equivalent to
flow.collect { doStuff(it) }
flow.launchIn(scope)
is equivalent to
scope.launch { flow.collect() }
So... if you want to use
launchIn
, you kind of have to go with
onEach
, because you don't control the
collect()
call anymore (you can't pass a lambda to it). I do tend to prefer
scope.launch { flow.collect { ... } }
in general, but I don't have a very strong opinion on this, I guess it's a matter of style. > I thought all of the "on*" operators were side-effecty Well
updateUI(event)
seems quite side-effecty to me 😄 It does seem to match what you expected here
👍 2
j
updateUI
is the direct effect, not a side effect
j
Ah I see. I was thinking in terms of something that changes state, as opposed to a pure function
r
One benefit of launchIn is it prevents you from mistakenly collecting without launching a separate coroutine, which would suspend the main coroutine.
h