What would be the correct way to launch another coroutine based on the value returned from a flow?
t
What would be the correct way to launch another coroutine based on the value returned from a flow?
m
Depends on the specific case. You case use filter+take(1) inside launch and do it sequentially i guess
You coul use launch inside onEach. The questions is very vague without an example
t
The question is the root of the issue above. Basically a ui state of logged in/out that is exposed via a StateFlow. If logged in, then connect to a web service. Connecting to the web service needs to run until the app view model is terminated. I haven't been able to find any examples of such a common use case.
a
Why do you need to launch another coroutine?
Flow.collect()
takes a suspend lambda which you can do your job right there.
t
When the user is logged in, I'll get the auth token that is needed to connect to a web socket. That web socket needs to run async until the user logs out or exists the app. If I connect in the collect lambda, then it will interfere with processing logout state changes.
a
You can use
collectLatest
then.