Skip to content

Instantly share code, notes, and snippets.

@ConorGarry
Last active May 11, 2020 07:07
Show Gist options
  • Save ConorGarry/628d951f225b20f8ab8fa91a240d5277 to your computer and use it in GitHub Desktop.
Save ConorGarry/628d951f225b20f8ab8fa91a240d5277 to your computer and use it in GitHub Desktop.
Extension functions for StateFlow
// Scope to LifeCycle ON_START
@ExperimentalCoroutinesApi
fun <T> LifecycleOwner.stateFlow(stateFlow: StateFlow<T>, funCollect: (T) -> Unit) {
lifecycleScope.launchWhenStarted {
stateFlow.collect {
funCollect(it)
}
}
}
// Called from Fragment
stateFlow(viewModel.testFlow) {
Timber.d("result: $it")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment