Posts

Showing posts with the label Kotlin Coroutines

Coroutines: RunBlocking Vs CoroutineScope

Answer : I don't understand how coroutineScope and runBlocking are different here? coroutineScope looks like its blocking since it only gets to the last line when it is done. From the perspective of the code in the block, your understanding is correct. The difference between runBlocking and coroutineScope happens at a lower level: what's happening to the thread while the coroutine is blocked? runBlocking is not a suspend fun . The thread that called it remains inside it until the coroutine is complete. coroutineScope is a suspend fun . If your coroutine suspends, the coroutineScope function gets suspended as well. This allows the top-level function, a non-suspending function that created the coroutine, to continue executing on the same thread. The thread has "escaped" the coroutineScope block and is ready to do some other work. In your specific example: when your coroutineScope suspends, control returns to the implementation code inside runBlocking . This...