stream that sequentially emits values and completes normally or with an exception. Flow fun call(): Flow<Int> = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value } } @Test fun runBlockingTest() = runBlocking { // Collect the flow foo().collect { value -> println(value) } } https://kotlinlang.org/docs/reference/coroutines/flow.html
flow { for (i in 1..3) { delay(100) emit(i) // emit next value } } fun runBlockingTest() { GlobalScope.launch { launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } foo().collect { value -> println(value) } } } @Test fun callTest() { println("subroutines") runBlockingTest() // Coroutines println("subroutines") }
= flow { for (i in 1..3) { delay(100) emit(i) // emit next value } } fun runBlockingTest() { GlobalScope.launch { launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } foo().collect { value -> println(value) } } } @Test fun callTest() { println("subroutines") runBlockingTest() // Coroutines println("subroutines") }
until its completion. This function should not be used from a coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests. runBlocking? https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html
೧ঠೠ. AAC ViewModel/LiveDataਸ ࢎਊೡ ࣻ Androidীࢲ ੜ ࢎਊೞӝ class MyViewModel: ViewModel() { init { viewModelScope.launch { // Coroutine that will be canceled when the ViewModel is cleared. } } } val user: LiveData<User> = liveData { val data = database.loadUser() // loadUser is a suspend function. emit(data) }