Hi everyone, I have a question regarding Flow coll...
# coroutines
d
Hi everyone, I have a question regarding Flow collection inside Fragment. My app heavily depends on Flow, and I have a lot of places where I need to collect it in UI layer, so I wanted to create an extension function to reduce the boilerplate code I need to write every time I have to collect it...
Copy code
inline fun <T> Fragment.collectFlowSafely(
    flowToCollect: Flow<T>,
    repeatState: Lifecycle.State = Lifecycle.State.STARTED,
    crossinline value: (T) -> Unit
) {
    viewLifecycleOwner.lifecycleScope.launch {
        viewLifecycleOwner.repeatOnLifecycle(repeatState) {
            flowToCollect.collect {
                value(it)
            }
        }
    }
}
Would this way of collecting flow be safe from cancellation point of view? i.e. Would cancel the collection when the view is no longer visible etc?
👍 1
👌 1