Sergio C.
02/10/2022, 12:43 PMviewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
locationProvider.locationFlow().collect {
// New location! Update the map
}
}
}
Semir Rahic
02/10/2022, 1:03 PMinline fun <T> Flow<T>.launchAndCollectIn(
owner: LifecycleOwner,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
crossinline action: suspend CoroutineScope.(T) -> Unit
) = owner.lifecycleScope.launch {
owner.repeatOnLifecycle(minActiveState) {
collect {
action(it)
}
}
}
Then call from Fragment like this:
viewModel.myFlow.launchAndCollectIn(this, Lifecycle.State.STARTED) {
//nasty stuff
}
There was a similar thing with repeatingJob
Sergio C.
02/10/2022, 1:04 PMSergio C.
02/10/2022, 1:05 PMSergio C.
02/10/2022, 1:06 PMSergio C.
02/10/2022, 1:07 PM-Xcontext-receivers
Where do we enable this flags?Semir Rahic
02/10/2022, 1:09 PMSemir Rahic
02/10/2022, 1:10 PMSergio C.
02/10/2022, 2:19 PM