Slide 1

Slide 1 text

Advanced Coroutine Concepts @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

Concurrency but... Structured 03 jobs! jobs! jobs! Jobs What are Jobs and what is their lifecycle... Builders How can you use builders to create coroutines Contexts What are contexts and what is context switching

Slide 4

Slide 4 text

The Builders What are builders and how can you use them

Slide 5

Slide 5 text

suited when no result is needed Scope.launch { ... } suited when a result is needed Scope.async { ... } What are coroutine builders? serve as extensions to CoroutineScope gives you more control over lifecycle and behaviour of a coroutine return a Job Functions which are used to launch coroutines

Slide 6

Slide 6 text

An Example Builders launch & async import kotlinx.coroutines.* suspend fun main() { val job = GlobalScope.launch { println("Searching for a Book") } val job2 = GlobalScope.async { println("Currently in the Library") delay(1000) "Found : Kotlin In Action Edition 2" } println(job) println(job2) println("-".repeat(20)) println(job2.await()) Thread.sleep(2000) } "coroutine#1":StandaloneCoroutine{Active}@5479e3f "coroutine#2":DeferredCoroutine{Active}@42f30e0a -------------------- Currently in the Library Searching for a Book Found : Kotlin In Action Edition 2 06

Slide 7

Slide 7 text

The Context What are contexts and what is context switching

Slide 8

Slide 8 text

dictates how the coroutines related to threads dispatcher represent the cancellable task needed to run job What's a coroutine context? represented CoroutineContext interface contain a Job and Dispatcher used to execute the coroutine Immutable key-value pair that contains various data available to the coroutine

Slide 9

Slide 9 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 06

Slide 10

Slide 10 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 11

Slide 11 text

The Jobs What are Jobs and what is their lifecycle

Slide 12

Slide 12 text

new created but not started start() / join() What are jobs? track task states and cancel check if isActive, isCancelled, isCompleted The lifecycle of the concurrent task active started but not completed completing waiting for children to complete completed finished and completed children

Slide 13

Slide 13 text

An Example Lifecycle check & update import kotlinx.coroutines.* suspend fun main() { val job = GlobalScope.launch { var i = 1 while(isActive){ println("Value : ${i++}") } } delay(20) job.cancel() } -------------------------------------- Value : 1 Value : 2 Value : 3 Value : 4 Value : 5 ... 06

Slide 14

Slide 14 text

Next Session Advanced Coroutine Concepts

Slide 15

Slide 15 text

Dispatchers Android specific dispatchers and where they are used In-Depth Guide to Android Coroutines 14 Scopes Different android specific scopes and where to use each 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