voben
12/05/2019, 6:46 PMlaunch
, launchWhenStarted
or launchWhenResumed
. Assuming I’m calling this in the fragment’s onViewCreated
viewLifecycleOwner.lifecycleScope.launchWhenResumed { }
tseisel
12/05/2019, 10:25 PMcollect
block. I'd say that all three are roughly equivalent in your case.
You can also use flow.onEach { ... }.launchIn(viewLifecycleOwner.lifecycleScope)
voben
12/05/2019, 10:38 PMscope.launch { flow.collect() }
Is the second one more kotlin-y than the first?Daniel
12/09/2019, 8:28 AMtseisel
12/09/2019, 8:54 AMflow.launchIn(scope)
does exactly the same thing as scope.launch { flow.collect() }
.
You may prefer the former because it could be called in the same operator chain:
flowOf(0, 1, 2, 3)
.filter { it % 2 == 0 }
.map { it * it }
.launchIn(scope)
VS
val flow = flowOf(0, 1, 2, 3)
.filter { it % 2 == 0 }
.map { it * it }
scope.launch { flow.collect() }