Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Collecting flows by using an extension function
/** Making this extension function cleans out the code a lot by removing the repetitive
flowWithLifecycle() or repeatOnLifecycle()
fun <T> Flow<T>.safeCollect(
owner: LifecycleOwner,
block: (T.() -> Unit)? = null
) = owner.lifecycleScope.launch {
flowWithLifecycle(owner.lifecycle).collectLatest { block?.invoke(it) }
//This Extension function can easily be used like
//Doing the work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment