The flow lifecycle article of <@UDFLD19DL> <https...
# android
o
The flow lifecycle article of @Manuel Vivo https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda gives this example of collecting multiple flows:
Copy code
// Listen to multiple flows
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                // As collect is a suspend function, if you want to collect
                // multiple flows in parallel, you need to do so in 
                // different coroutines
                launch {
                    flow1.collect { /* Do something */ }   
                }
                
                launch {
                    flow2.collect { /* Do something */ }
                }
            }
        }
Is there anything wrong with doing this instead? :
Copy code
// Listen to multiple flows
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                // As collect is a suspend function, if you want to collect
                // multiple flows in parallel, you need to do so in 
                // different coroutines
                launch {
                    flow1.collect { /* Do something */ }   
                }
            
            }
        }
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                
                launch {
                    flow2.collect { /* Do something */ }
                }
            }
        }
a
You can skip the extra inner launches in the second example since each
repeatOnLifecycle
block is only doing one thing at a time, other than that, yeah that's fine
👍 1
i
@Adam Powell I am confused in this scenario: Should I launch repeatOnLifecycle(Started) in a normal lifecycleScope.launch or launchWhenStarted?
a
Avoid
launchWhenStarted
altogether. It was a bit of a failed experiment and will be removed in a future release eventually.