Skip to content

Instantly share code, notes, and snippets.

@putrarifin
Created December 4, 2020 17:44
Show Gist options
  • Save putrarifin/3b96126acd5f2b87160b5a33acb45605 to your computer and use it in GitHub Desktop.
Save putrarifin/3b96126acd5f2b87160b5a33acb45605 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
val stateFlow = MutableStateFlow<Int>(0)
// Observe values
val job = launch {
stateFlow.collect {
print("$it ")
}
}
// Change values
(1..5).forEach {
delay(500)
stateFlow.value = it
}
// Cancel running job
job.cancel()
job.join()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment