简体   繁体   中英

Kotlin coroutines UI not freezing

I've checking the livedata coroutines lately and i get to something i cannot understand. why is this code (When observed) not freezing the UI?

    val lv =
    liveData (context=Dispatchers.Main){
        var x = 0
        while (true){
            emit(x++)
            delay(1000)
            println(Thread.currentThread().name)
        }
    }

Thanks.

delay doesn't block the thread. It just suspends your coroutine and resumes it after one second. You can change delay to Thread.sleep(1000) and your UI will be frozen because Thread.sleep will block the thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM