What would be the correct way to launch another coroutine based on the value returned from a flow?
m
myanmarking
04/27/2022, 9:00 PM
Depends on the specific case. You case use filter+take(1) inside launch and do it sequentially i guess
myanmarking
04/27/2022, 9:01 PM
You coul use launch inside onEach. The questions is very vague without an example
t
Trey
04/27/2022, 9:05 PM
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
Albert Chang
04/28/2022, 4:15 AM
Why do you need to launch another coroutine?
Flow.collect()
takes a suspend lambda which you can do your job right there.
t
Trey
04/28/2022, 2:49 PM
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.