Slide 1

Slide 1 text

In-Depth Guide For Coroutines In Android @mambo_bryan 01

Slide 2

Slide 2 text

02 Design Centric Engineer Brian Odhiambo Dishwasher @ Baobab Circle Co-organizer @ KotlinKenya Maintaining @ KotlinBits Yeah am all about that UI/UX & Kotlin

Slide 3

Slide 3 text

Scope Concurrency 03 viewmodels! lifecycle! dispatchers! Dispatchers Android specific dispatchers and where they are used Scopes Different android specific scopes and where to use each

Slide 4

Slide 4 text

android devices have relatively small memory Little Memory all ui changes run on the main The UI & Main Thread Something to NOTE there has to be cooperation and collaboration Structured Concurrency

Slide 5

Slide 5 text

Dispatchers What do we have available

Slide 6

Slide 6 text

UI or Non-Blocking code Dispatchers.Main Network and Disk operations Dispatchers.IO What dispatchers do we have available? CPU intensive tasks Dispatchers.Default inherits dispatcher that called it Dispatchers.Unconfined

Slide 7

Slide 7 text

An Example Dispatchers import kotlinx.coroutines.* suspend fun main() { withContext(Dispatchers.Main) { ... } withContext(Dispatchers.Unconfined) { ... } withContext(Dispatchers.IO) { ... } withContext(Dispatchers.Default) { ... } } 06

Slide 8

Slide 8 text

Scopes How do scopes affect our apps

Slide 9

Slide 9 text

what is the job's lifecycle? who will cancel the job? who handles the exceptions thrown? Structured Concurrency

Slide 10

Slide 10 text

tied to the lifecycle of the viewmodel viewModelScope tied to the lifecycle state of the component. Either activity or fragment lifecycleScope Different Out of The Box Scopes onCreate onStart onResume onPause onStop onDestroy

Slide 11

Slide 11 text

An Example Coroutine context in purple import kotlinx.coroutines.* fun CoroutineScope.working(){ println("I'm working in thread ${Thread.currentThread().name}") } fun main() = runBlocking { 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

Slide 12

Slide 12 text

What is context switching? running a coroutine in a different 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() } }

Slide 13

Slide 13 text

Let's Jump To The Good Stuff Android studio!!!

Slide 14

Slide 14 text

Next Session Advanced Coroutine Concepts

Slide 15

Slide 15 text

Actual Testing at it's finest Testing Coroutines 14 Expected How do we test and so much more!

Slide 16

Slide 16 text

One Last Thing For the community

Slide 17

Slide 17 text

KotlinBits 😎 A fun and easy way to learn Kotlin in small bits and pieces. React, Comment & Share https://kotlinbits.vercel.app

Slide 18

Slide 18 text

16 mambo_bryan MamboBryan Thank you! Have a nice Kotlin KotlinBits kotlinbits.vercel.app