Slide 1

Slide 1 text

The Subtle Bliss of Coroutines @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

HOW How to use coroutines WHY Why use coroutines The World of Concurrency 03 WHAT What are coroutines synchronous! asynchronous! structured!

Slide 4

Slide 4 text

The WHAT? What are coroutines

Slide 5

Slide 5 text

Works concurrently with the rest of the code Similar to threads but lightweight Is not tied to any thread Expressed in a structured way What are coroutines? An instance of suspendable computation

Slide 6

Slide 6 text

An Example Coroutines marked with suspend import kotlinx.coroutines.* suspend fun main() { println("Starting Work") doWork() } suspend fun doWork(){ delay(1000) println("Finished Work") } Starting Work Finished Work 06

Slide 7

Slide 7 text

The WHY? Why coroutines

Slide 8

Slide 8 text

Prevents the callback hell problem Easily maintainable & scalable Gives you more control of how you handle jobs Less resource intensive than JVM threads (2mb) Why Coroutines ? sendRequest( onSuccess = { sendAnotherRequest( onSuccess = {...}, onFailure = {...}, ) }, onFailure = {...} )

Slide 9

Slide 9 text

Why Coroutines ? FROM : sendRequest( onSuccess = { sendAnotherRequest( onSuccess = {...}, onFailure = {...}, ) }, onFailure = {...} ) TO : val result = sendRequest() if(response != null){ val result = sendAnotherResponse() ... } ...

Slide 10

Slide 10 text

The HOW? How do you coroutine

Slide 11

Slide 11 text

Example custom coroutine marked with suspend import kotlinx.coroutines.* fun main() { println("Starting Work") GlobalScope.launch { doWork() } Thread.sleep(2000) } suspend fun doWork(){ delay(1000) println("Finished Work") } Starting Work Finished Work 11

Slide 12

Slide 12 text

Example II custom coroutine marked with suspend import kotlinx.coroutines.* fun main() { println("Starting Work | ${Thread.currentThread().name}") GlobalScope.launch { doWork("1") } CoroutineScope(Dispatchers.IO).launch { doWork("2") } Thread.sleep(3000) } suspend fun doWork(name: String){ delay(1000) println("Finished Work $name | ${Thread.currentThread().name}") } Starting Work | main Finished Work 1 | DefaultDispatcher-worker-2 @coroutine#1 Finished Work 2 | DefaultDispatcher-worker-2 @coroutine#2 12

Slide 13

Slide 13 text

Next Session Advanced Coroutine Concepts

Slide 14

Slide 14 text

Jobs What are Jobs and what is their lifecycle... Builders How can you use builders to create coroutines Advanced Coroutine Concepts 14 Contexts What are contexts and what is context switching and so much more!

Slide 15

Slide 15 text

One Last Thing For the community

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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