Skip to content

Instantly share code, notes, and snippets.

@Vivekban
Created March 18, 2024 13:28
Show Gist options
  • Save Vivekban/68890aa93fb56473f18ae717b0f50073 to your computer and use it in GitHub Desktop.
Save Vivekban/68890aa93fb56473f18ae717b0f50073 to your computer and use it in GitHub Desktop.
Combing state and shared flow
suspend fun flowCombineFix() {
val sharedFlow = MutableSharedFlow<String>()
val stateFlow = MutableStateFlow<Int>(0)
combine(
// Emit default value on start
sharedFlow.onStart { emit("") },
stateFlow
) { shared, state ->
"$shared $state"
}.collect {
Log.d(TAG, "Result is $it")
}
stateFlow.emit(1)
stateFlow.emit(2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment