Last active
May 11, 2020 07:07
-
-
Save ConorGarry/628d951f225b20f8ab8fa91a240d5277 to your computer and use it in GitHub Desktop.
Extension functions for StateFlow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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