Skip to content

Instantly share code, notes, and snippets.

@gab-stargazer
Last active December 6, 2023 11:24
Show Gist options
  • Save gab-stargazer/45bef322a773206299a13c2b6886de1a to your computer and use it in GitHub Desktop.
Save gab-stargazer/45bef322a773206299a13c2b6886de1a to your computer and use it in GitHub Desktop.
Handle Flow On LaunchedEffect with Jetpack Compose
@Composable
inline fun <reified T> Flow<T>.CollectInLaunchEffect(
key: Any,
noinline block: suspend (T) -> Unit
) {
val lifeCycle = LocalLifecycleOwner.current.lifecycle
LaunchedEffect(key1 = key) {
lifeCycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
this@CollectInLaunchEffect.collectLatest(block)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment