Coroutines III : In-Depth Guide For Coroutines In Android
These are the slides for the third session on coroutines. In this session I cover android specific coroutine scopes, available dispatchers and where they are used. And a code along of how it works.
the lifecycle state of the component. Either activity or fragment lifecycleScope Different Out of The Box Scopes onCreate onStart onResume onPause onStop onDestroy
println("I'm working in thread ${Thread.currentThread().name}") } fun main() = runBlocking<Unit> { launch { working() } launch(Dispatchers.Unconfined) { working() } launch(Dispatchers.Default) { working() } launch(CoroutineName("MyOwnThread")) { working() } } I'm working in thread main @coroutine#3 I'm working in thread DefaultDispatcher-worker-1 @coroutine#4 I'm working in thread main @coroutine#2 I'm working in thread main @MyOwnThread#5 09
thread Changing the context in which a coroutine runs Example import kotlinx.coroutines.* suspend fun main(){ doNetworkStuff() } suspend fun updateUi(){ withContext(Dispatchers.Main){ ... } } suspend fun doNetworkStuff(){ withContext(Dispatchers.IO){ ... updateUi() } }