Created
December 4, 2020 17:44
-
-
Save putrarifin/3b96126acd5f2b87160b5a33acb45605 to your computer and use it in GitHub Desktop.
This file contains 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
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