Slide 1

Slide 1 text

Bob Dahlberg Cracking Kotlin Coroutines

Slide 2

Slide 2 text

Suspension Builders Scope Context Structured Concurrency Job Cancellation Exception Handling Streams* Cracking Kotlin Coroutines

Slide 3

Slide 3 text

Cheating

Slide 4

Slide 4 text

Cheating // clears the text field clear(optional:String = ””) // variants adds to the text field out(message:Any) // launches a coroutine viewModelScope.launch {}

Slide 5

Slide 5 text

Suspension

Slide 6

Slide 6 text

Suspension Suspension is one of the most central and fundamental part of Kotlin coroutines. Suspending is pausing Suspending is resuming Dividing the coroutine into smaller pieces

Slide 7

Slide 7 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 8

Slide 8 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 9

Slide 9 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 10

Slide 10 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 11

Slide 11 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 12

Slide 12 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 13

Slide 13 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 14

Slide 14 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 15

Slide 15 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 16

Slide 16 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 17

Slide 17 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 18

Slide 18 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 19

Slide 19 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 20

Slide 20 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) }

Slide 21

Slide 21 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) } // 0, 1, 6, 2, 4, 7, 3, 5

Slide 22

Slide 22 text

Suspension fun order() { out(0) viewModelScope.launch { out(1) launch { out(2) delay(100) out(3) } launch { out(4) delay(100) out(5) } out(6) } out(7) } // 0, 1, 6, 2, 4, 7, 3, 5

Slide 23

Slide 23 text

Suspension Lets look closer at the continuation in action with the suspendCancellableCoroutine function. fun simpleContinuation() { viewModelScope.launch { out(1) suspendCancellableCoroutine { out(2)
 it.resumeWith(Result.success(2)) } out(3) } }

Slide 24

Slide 24 text

Suspension Use suspendCancellableCoroutine instead of suspendCoroutine, Useful when bridging between callbacks and coroutines. They are suspending functions, and as all suspending functions - they need to be called from a suspending function, and so on. So where to start?

Slide 25

Slide 25 text

Builders

Slide 26

Slide 26 text

val job = launch { } val deferred = async { } Builders

Slide 27

Slide 27 text

Scope

Slide 28

Slide 28 text

Scope A very small interface, only holding a reference to the context. Don't be afraid to create them - they are not dangerous. Look at viewModelScope to see an example used a lot But don't overuse them

Slide 29

Slide 29 text

Context

Slide 30

Slide 30 text

Context Simpli fi ed - A HashMap of Contexts We need a context - it de fi nes the rules for our coroutine We can override any value of the context Job, Dispatchers, CoroutineExceptionHandler, CoroutineName to name a few

Slide 31

Slide 31 text

Structured concurrency

Slide 32

Slide 32 text

Structured concurrency An important and often overlooked part of coroutines. Our builders are extension functions to the CoroutineScope. Creating a new coroutine (invoking a builder) inherits the context and creates a parent/child relationship. Each coroutine awaits it’s children before completing

Slide 33

Slide 33 text

Job

Slide 34

Slide 34 text

Job Has a lifecycle dictating the state Each coroutine has it’s own job! It’s the only context that isn’t inherited 
 - each coroutine creates Its own instance with the inherited as its parent. Creating the hierarchy needed for structured concurrency

Slide 35

Slide 35 text

Job Has a lifecycle dictating the state Each coroutine has it’s own job! It’s the only context that isn’t inherited 
 - each coroutine creates Its own instance with the inherited (or overridden) as its parent. Creating the hierarchy needed for structured concurrency

Slide 36

Slide 36 text

Job wait children +-----+ start +--------+ complete +-------------+ finish +-----------+ | New | -----> | Active | --------> | Completing | -------> | Completed | +-----+ +--------+ +-------------+ +-----------+ | cancel / fail | | +----------------+ | | V V +------------+ finish +-----------+ | Cancelling | -------------------------------> | Cancelled | +------------+ +-----------+

Slide 37

Slide 37 text

Cancellation

Slide 38

Slide 38 text

Cancellation Propagation Tidying up Collaborative Rules

Slide 39

Slide 39 text

Exceptions

Slide 40

Slide 40 text

Exceptions Propagation Tidying up Rules CoroutineExceptionHandler

Slide 41

Slide 41 text

Streams

Slide 42

Slide 42 text

Streams Flows Flows adhere to the general cooperative cancellation of coroutines. As usual, fl ow collection can be cancelled when the fl ow is suspended in a cancellable suspending function (like delay). Channels Note: Ticker channels are not currently integrated with structured concurrency and their api will change in the future.

Slide 43

Slide 43 text

Sum up

Slide 44

Slide 44 text

Suspension
 - They de fi ne the units of code
 - suspendCancellableCoroutine
 - Can only be called from other suspending functions Scope
 - They are the logical root of coroutines
 - Needed for builders
 - Cancel children, not the scope
 - Think of viewModelScope when in doubt. Structured concurrency
 - Understand it! Utilize it!
 - Even children with di ff erent dispatchers are awaited
 - Don’t think in callbacks. Job
 - The controller of structured concurrency
 - Understand the lifecycle
 - Be ware of creation to not outplay the structure
 - One coroutine - One Job Sum up

Slide 45

Slide 45 text

Cancellation
 - They are collaborative - on suspension
 - Remember to re-trow them Exceptions
 - SupervisorJob / Job di ff erences
 - Use supervisorScope instead of creating jobs.
 - Propagates to the root (scope’s handler)
 - withContext(NonCancellable) Streams
 - Flows are built on top of coroutines
 - Channels are used to communicate between coroutines
 - Channels can’t broadcast
 - Mind context and use fl owOn
 - Think about order when collecting fl ows Sum up

Slide 46

Slide 46 text

Essentially: the Job is key,
 honouring structured concurrency,
 and grasping the suspending mechanism Sum up

Slide 47

Slide 47 text

Thank you

Slide 48

Slide 48 text

Questions? Thank you! Bob Dahlberg medium.com/dahlbergbob @mr_bob