Slide 1

Slide 1 text

Rivu Chakraborty Efficient Async Coding in Kotlin Coroutines 1 Bangalore JAVA User Group (BoJUG) - Year’s First Meetup - Full Day - Informatica - Jan, 19, 2019 19/01/19

Slide 2

Slide 2 text

Rivu Chakraborty BYJU’S @rivuchakraborty Efficient Async Coding in Kotlin Coroutines 2

Slide 3

Slide 3 text

Rivu Chakraborty BYJU’S @rivuchakraborty About Me ● Sr Software Engineer (Android) - BYJU’S ● Instructor - Caster.io ● Google Certified Associate Android Developer ● DroidJam Speaker ● Author - Reactive Programming in Kotlin ● Author - Functional Kotlin ● Author - Coroutines for Android Developers (WIP)

Slide 4

Slide 4 text

@rivuchakraborty

Slide 5

Slide 5 text

Books https://www.packtpub.com/application-de velopment/reactive-programming-kotlin https://www.packtpub.com/application-dev elopment/functional-kotlin https://leanpub.com/coroutines-for-androi d-developers Work in Progress

Slide 6

Slide 6 text

Efficient Async Coding in Kotlin Coroutines Async => Concurrency

Slide 7

Slide 7 text

Concurrency ● Ability to execute multiple code blocks at the same time @rivuchakraborty

Slide 8

Slide 8 text

Concurrency ● Ability to execute multiple code blocks at the same time ● Not only for Android Developers @rivuchakraborty

Slide 9

Slide 9 text

Concurrency ● Ability to execute multiple code blocks at the same time ● Not only for Android Developers ● JavaScript - Promise ● JVM, Android - Threads @rivuchakraborty

Slide 10

Slide 10 text

10 Concurrency in Android and/or JVM ● AsyncTask (Android only) ● CompletableFuture (Android - API 24+, JVM 1.8) ● Runnable / Thread (Custom Threadpool) ● Rx ● Counting... @rivuchakraborty

Slide 11

Slide 11 text

11 Concurrency in Android ● Runnable / Thread @rivuchakraborty

Slide 12

Slide 12 text

12 Concurrency in Android ● Rx @rivuchakraborty

Slide 13

Slide 13 text

13 Concurrency in Android ● Rx @rivuchakraborty If you’re using Rx only for concurrency, sorry pal, you’re doing it wrong.

Slide 14

Slide 14 text

14 ● Coroutines @rivuchakraborty Let’s you write non-blocking asynchronous code in your choice of Style - Sequentially, in Functional Style or whatever you prefer.

Slide 15

Slide 15 text

15 ● Coroutines @rivuchakraborty Let’s you write non-blocking asynchronous code in your choice of Style - Sequentially, in Functional Style or whatever you prefer. A Language level feature, managed by the Kotlin team itself.

Slide 16

Slide 16 text

16 ● Coroutines @rivuchakraborty ● Let’s you write non-blocking asynchronous code in your choice of Style - Sequentially, in Functional Style or whatever you prefer. ● A Language level feature, managed by the Kotlin team itself. You can add Coroutines dependency to your Kotlin project by following the instructions in GitHub ReadMe - https://github.com/kotlin/kotlinx.coroutines/blob/master/README.md#u sing-in-your-projects

Slide 17

Slide 17 text

Coroutines 17 @rivuchakraborty Light-Weight Threads Thread 1 Thread 2 Thread 3 Coroutine 1 Coroutine 2 Coroutine 3 With Coroutines, Threads are still used (for JVM).

Slide 18

Slide 18 text

Coroutines 18 @rivuchakraborty Light-Weight Threads Thread 1 Thread 2 Thread 3 Coroutine 1 Coroutine 2 C 3 C 4 C 5 C 6 C 7 C 8 C N But a single Thread can run multiple coroutines.

Slide 19

Slide 19 text

Coroutines 19 @rivuchakraborty Easy to Use

Slide 20

Slide 20 text

Suspension & Continuation Builders Suspending Functions End Coroutines Components Scope & Dispatchers @rivuchakraborty

Slide 21

Slide 21 text

Coroutines 21 @rivuchakraborty Suspending vs Blocking

Slide 22

Slide 22 text

Coroutines 22 @rivuchakraborty Suspending vs Blocking

Slide 23

Slide 23 text

Coroutines 23 @rivuchakraborty Suspending vs Blocking

Slide 24

Slide 24 text

Coroutines 24 @rivuchakraborty Suspending vs Blocking

Slide 25

Slide 25 text

Launch a Coroutine 25 @rivuchakraborty

Slide 26

Slide 26 text

Get the Job done or cancel it, your choice. :) 26 @rivuchakraborty

Slide 27

Slide 27 text

Compute Async(hrounously) 27 @rivuchakraborty

Slide 28

Slide 28 text

Deferred 28 @rivuchakraborty ● Deferred Extends Job ● Wrapper around your data

Slide 29

Slide 29 text

Suspending Function 29 @rivuchakraborty ● suspend is a Keyword in Kotlin

Slide 30

Slide 30 text

Suspending Function 30 @rivuchakraborty ● suspend is a Keyword in Kotlin ● Compiler level restriction - can’t call suspend function outside CoroutineScope

Slide 31

Slide 31 text

Suspending Function 31 @rivuchakraborty ● suspend is a Keyword in Kotlin ● Compiler level restriction - can’t call suspend function outside CoroutineScope ● Suspends execution of current coroutine

Slide 32

Slide 32 text

Coroutine Scope 32 @rivuchakraborty ● Container of CoroutineContext

Slide 33

Slide 33 text

Coroutine Scope 33 @rivuchakraborty ● Container of CoroutineContext ● Every Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext

Slide 34

Slide 34 text

Coroutine Scope 34 @rivuchakraborty ● Container of CoroutineContext ● Every Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext ● All Coroutines must be launched within a CoroutineScope

Slide 35

Slide 35 text

Coroutine Scope 35 @rivuchakraborty

Slide 36

Slide 36 text

Dispatchers 36 @rivuchakraborty ● Determines which Thread / ThreadPool, the coroutine will run on. ● Similar to the Rx Schedulers

Slide 37

Slide 37 text

Let’s Build for Android

Slide 38

Slide 38 text

Repository 38 @rivuchakraborty

Slide 39

Slide 39 text

API (Retrofit) 39 @rivuchakraborty

Slide 40

Slide 40 text

Activity 40 @rivuchakraborty

Slide 41

Slide 41 text

Call it 41 @rivuchakraborty

Slide 42

Slide 42 text

Handle Exception (Gracefully) :) 42 @rivuchakraborty

Slide 43

Slide 43 text

Rivu Chakraborty @rivuchakraborty Resources https://caster.io/courses/kotlin-coroutines-fundamental s ● http://bit.ly/CodeLabCoroutines (Codelab) ● http://bit.ly/CoroutinesAndroid (Article) ● http://bit.ly/DroidCoroutines (Book, yet to be published) ● http://bit.ly/CoroutinesGuide (Official Guide) ● https://www.meetup.com/BlrKotlin/ (Bangalore Kotlin User Group) 43

Slide 44

Slide 44 text

Rivu Chakraborty @rivuchakraborty Thank you! 44