Bob Dahlberg
Android at Bonnier News
Challenge Accepted
Kotlin Stockholm Tech Group
GDG Stockholm Android
Who’s Bob?
- Building software
- CrossFit enthusiast
- Father of one
- Arranging meetups
- Loves a good challenge!
Slide 3
Slide 3 text
Background
ActionScript (Flash)
Been there! Done that! Moved …
Slide 4
Slide 4 text
Background
ActionScript (Flash)
Been there! Done that! Moved …
For all of you born after the
millennium, flash was king!
Slide 5
Slide 5 text
Background
ActionScript (Flash)
Slide 6
Slide 6 text
Background
ActionScript
Android (Java)
Started something new since
flash was about to be abandoned.
Went native with Java rather
than cross platform:
(ActionScript, Xamarin,
PhoneGap)
Slide 7
Slide 7 text
Background
ActionScript
Android (Java)
Did a lot of threading,
networking, image loading with
different solutions.
Built my own async task handler.
Slide 8
Slide 8 text
Background
ActionScript
Android
Rx
Did a lot of threading,
networking, image loading with
different solutions.
Built my own async task handler.
Ended up doing a lot of Rx.
Slide 9
Slide 9 text
Background
ActionScript
Android
Rx
Scala
Learned Scala and tried to build
Android with it..
Slide 10
Slide 10 text
Background
ActionScript
Android
Rx
Scala
Learned Scala and tried to build
Android with it..
It’s plausible.
But you rather die a little.
Slide 11
Slide 11 text
Background
ActionScript
Android
Rx
Scala
Kotlin
Found Kotlin in 2015 and it
blended almost seamlessly with
Java.
Slide 12
Slide 12 text
Background
ActionScript
Android
Rx
Scala
Kotlin
So why all this about my past?
Slide 13
Slide 13 text
Background
ActionScript - Asynchronous
Android - Asynchronous
Rx - Asynchronous
Scala. - Functional
Kotlin - Functional
I’m raised in an asynchronous,
callback world.
With strong beliefs in reactive
and functional idioms.
Even before I knew it.
Slide 14
Slide 14 text
Background
ActionScript - Asynchronous
Android - Asynchronous
Rx - Asynchronous
Scala. - Functional
Kotlin - Functional
Cause of that coroutines are
hard for my brain to grasp.
Slide 15
Slide 15 text
Coroutines
Slide 16
Slide 16 text
Coroutines
Lightweight threads
Conceptually the same as threads.
- Offload async work on them
- Specific ways of communication
Slide 17
Slide 17 text
Coroutines
Lightweight threads
Structured
Structured concurrency
“An outer coroutine does not
complete until all the coroutines
launched in its scope complete”
in its scope
Slide 18
Slide 18 text
Coroutines
Lightweight threads
Structured
Slide 19
Slide 19 text
Coroutines
Lightweight threads
Structured
Slide 20
Slide 20 text
Coroutines
Lightweight threads
Structured
Slide 21
Slide 21 text
Coroutines
Lightweight threads
Structured
Slide 22
Slide 22 text
Coroutines
Lightweight threads
Structured
Suspend
Coroutines are built by
suspending functions.
They suspend the current scope
until done.
Slide 23
Slide 23 text
Coroutines
Lightweight threads
Structured
Suspend
Slide 24
Slide 24 text
Coroutines
Lightweight threads
Structured
Suspend
Slide 25
Slide 25 text
Coroutines
Lightweight threads
Structured
Suspend
Slide 26
Slide 26 text
Coroutines
Lightweight threads
Structured
Suspend
Sequential
Coroutines are sequential by
default.
Non blocking, asynchronous.
But not concurrent.
Coroutines
Lightweight threads
Structured
Suspend
Sequential
Dispatchers
“The coroutine context includes
a coroutine dispatcher that
determines what thread or threads
the corresponding coroutine uses
for its execution.”
Coroutines
Lightweight threads
Structured
Suspend
Sequential
Dispatchers
Dispatchers.Default - 8 threads
Dispatchers.IO - 64 threads
Dispatchers.Main - 0 or 1 threads
On this machine!
Slide 33
Slide 33 text
Coroutines
Comparison
Slide 34
Slide 34 text
Comparison
Callbacks
Slide 35
Slide 35 text
Comparison
Callbacks
Rx
Slide 36
Slide 36 text
Comparison
Callbacks
Rx
Coroutines
Slide 37
Slide 37 text
So are coroutines replacing Rx?!
Slide 38
Slide 38 text
So are coroutines replacing Rx?!
Nope!
Slide 39
Slide 39 text
So are coroutines replacing Rx?!
Nope!
It can replace some.
But coroutines is not reactive streams.
Slide 40
Slide 40 text
Coroutines
Sharing is caring
Slide 41
Slide 41 text
Communication
Shared mutable state
Still something that should be
handled with extreme caution!
Coroutines gives you nothing new
on that matter.
Slide 42
Slide 42 text
Communication
Shared mutable state
launch vs async
Both launch and async coroutine
builders returning a job for the
calling scope to control
(cancel - join).
Slide 43
Slide 43 text
Communication
Shared mutable state
launch vs async
Slide 44
Slide 44 text
Communication
Shared mutable state
launch vs async
But async gives us a bit more.
Slide 45
Slide 45 text
Communication
Shared mutable state
launch vs async
Deferred
Slide 46
Slide 46 text
Communication
Shared mutable state
launch vs async
Deferred
Slide 47
Slide 47 text
Communication
Shared mutable state
launch vs async
Deferred
Slide 48
Slide 48 text
Communication
Shared mutable state
launch vs async
Deferred
Slide 49
Slide 49 text
Communication
Shared mutable state
launch vs async
Deferred
Order
Slide 50
Slide 50 text
Communication
Shared mutable state
launch vs async
Deferred
Order
Slide 51
Slide 51 text
Communication
Shared mutable state
launch vs async
Deferred
Order
Slide 52
Slide 52 text
Communication
Shared mutable state
launch vs async
Deferred
Order
Slide 53
Slide 53 text
Communication
Shared mutable state
launch vs async
Deferred
Order
Slide 54
Slide 54 text
Communication
Shared mutable state
launch vs async
Deferred
Order
Streams
Slide 55
Slide 55 text
Communication
Shared mutable state
launch vs async
Deferred
Order
Streams
Slide 56
Slide 56 text
Channels
Slide 57
Slide 57 text
Channels
Definition
“Deferred values provide a
convenient way to transfer a
single value between coroutines.
Channels provide a way to
transfer a stream of values.”
Slide 58
Slide 58 text
Channels
Definition
Streams revisited
Slide 59
Slide 59 text
Channels
Definition
Streams revisited
Slide 60
Slide 60 text
Channels
Definition
Streams revisited
Slide 61
Slide 61 text
Channels
Definition
Streams revisited
Order revisited
Slide 62
Slide 62 text
Channels
Definition
Streams revisited
Order revisited
Slide 63
Slide 63 text
Channels
Definition
Streams revisited
Order revisited
Slide 64
Slide 64 text
Channels
Definition
Streams revisited
Order revisited
Slide 65
Slide 65 text
Channels
Definition
Streams revisited
Order revisited
Slide 66
Slide 66 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Slide 67
Slide 67 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Slide 68
Slide 68 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Plain order of execution should
not be handled by channels.
Slide 69
Slide 69 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
But Channels are
synchronization primitives…
Slide 70
Slide 70 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
But Channels are
synchronization primitives…
So there must be some order!
Slide 71
Slide 71 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Order - Producer / Consumer
Slide 72
Slide 72 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Order - Producer / Consumer
Slide 73
Slide 73 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Order - Producer / Consumer
Slide 74
Slide 74 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Order - Producer / Consumer
Slide 75
Slide 75 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Order - Producer / Consumer
Experimental
Slide 76
Slide 76 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Order - Producer / Consumer
Experimental
Slide 77
Slide 77 text
Channels
Definition
Streams revisited
Order revisited
Order revisited 2
Order revisited 3
Order - Producer / Consumer
Experimental
A friend once told me that if
it’s on a single line it can’t
be bad code.
Slide 78
Slide 78 text
Channels
Will it terminate?
Slide 79
Slide 79 text
Will it terminate
Slide 80
Slide 80 text
Will it terminate
Slide 81
Slide 81 text
Will it terminate
Slide 82
Slide 82 text
Will it terminate
Slide 83
Slide 83 text
Will it terminate
Slide 84
Slide 84 text
Gotchas
Slide 85
Slide 85 text
Gotchas
Send is suspending
Slide 86
Slide 86 text
Gotchas
Send is suspending
Buffer
Slide 87
Slide 87 text
Gotchas
Send is suspending
Buffer
Rendezvous
Slide 88
Slide 88 text
Gotchas
Send is suspending
Buffer
Rendezvous
Unlimited
Slide 89
Slide 89 text
Gotchas
Send is suspending
Buffer
Rendezvous
Unlimited
Conflated
Slide 90
Slide 90 text
Gotchas
Send is suspending
Buffer
Rendezvous
Unlimited
Conflated
Channels are hot
Slide 91
Slide 91 text
Channels
“Primitive” usages
Slide 92
Slide 92 text
Channels
Fan out
One producer, many consumers.
Slide 93
Slide 93 text
Channels
Fan out
Slide 94
Slide 94 text
Channels
Fan out
Slide 95
Slide 95 text
Channels
Fan out
Fan in
One consumer, many producers.
Slide 96
Slide 96 text
Channels
Fan out
Fan in
Slide 97
Slide 97 text
Channels
Fan out
Fan in
Slide 98
Slide 98 text
Channels
Fan out
Fan in
FIFO
Channels respect the order of
invocations from different
coroutines.
Elements are received in a
“first in first out” order.
Slide 99
Slide 99 text
Channels
Fan out
Fan in
FIFO
Channels respect the order of
invocations from different
coroutines.
Elements are received in a
“first in first out” order.
Slide 100
Slide 100 text
Channels
Builders and Types of Channels
Slide 101
Slide 101 text
Creating
Channel
The simple Channel() standard
and simple.
Also ReceiverChannel and
SendChannel for specific usage
Slide 102
Slide 102 text
Creating
Channel
Broadcast?
As seen especially in the fan
out example where we have many
consumers and one producer,
kinda like RX.
But there’s only one receiving
each element… can we broadcast?
Slide 103
Slide 103 text
Creating
Channel
BroadcastChannel
As seen especially in the fan
out example where we have many
consumers and one producer,
kinda like RX.
But there’s only one receiving
each element… can we broadcast?
Yes, we can!
Slide 104
Slide 104 text
Creating
Channel
BroadcastChannel
ConflatedBroadcastChannel
Now we can also have only the
most recent sent value.
Did anyone say BehaviorSubject?
Slide 105
Slide 105 text
Creating
Channel
BroadcastChannel
ConflatedBroadcastChannel
Operators?
To really become a force to
compare to Rx we must have some
operators!
Actor
Should match
Like actors that encapsulates a
shared mutable state.
Their mailbox should be a good
fit for channels!
Slide 117
Slide 117 text
Actor
Should match
Actor builder
Slide 118
Slide 118 text
Actor
Should match
Actor builder
Slide 119
Slide 119 text
Actor
Should match
Actor builder
Slide 120
Slide 120 text
ObsoleteApi
Meaning it will be removed
Slide 121
Slide 121 text
Why?
Slide 122
Slide 122 text
Why?
Why is everything deprecated or
obsolete?
Slide 123
Slide 123 text
Why?
Actor builder
“Note: This API will become
obsolete in future updates with
introduction of complex actors.”
Slide 124
Slide 124 text
Why?
Actor builder
All the operators
“Channel operators are
deprecated in favour of Flow and
will be removed in 1.4”
Slide 125
Slide 125 text
What is Flow?
Slide 126
Slide 126 text
Flow
Cold streams Cold streams, some using
channels to communicate.
Slide 127
Slide 127 text
Flow
Cold streams
On coroutines
Running on coroutines and
benefits from not having
subscribers and cleaning up
duties.
Slide 128
Slide 128 text
Flow
Cold streams
On coroutines
Extensible
Loads of operators built in
and extremely easy to add
via extension functions.
Slide 129
Slide 129 text
Flow
Cold streams
On coroutines
Extensible
Channels
Giving them the benefit of back
pressure support easy as that.
Slide 130
Slide 130 text
Is it a replacement for Rx?
Slide 131
Slide 131 text
Is it a replacement for Rx?
Slide 132
Slide 132 text
Is it a replacement for Rx?
Yup!
(IMHO)
Slide 133
Slide 133 text
To sum up
Slide 134
Slide 134 text
Sum up
Coroutines
Not a replacement of Rx
Slide 135
Slide 135 text
Sum up
Coroutines
Channels
Not a replacement of Rx
Not a replacement of Rx
Slide 136
Slide 136 text
Sum up
Coroutines
Channels
Flow
Not a replacement of Rx
Not a replacement of Rx
A replacement of Rx
Slide 137
Slide 137 text
Take aways
Coroutines
It’s one of the cleanest ways to
work with asynchronous code.
Even though I’m still having
issues to wrap my head around
the new way of thinking.
You know, old dogs!
Slide 138
Slide 138 text
Take aways
Coroutines
Deferred
A synchronisation primitive
solving the output of one value
or one object from a coroutine.
Slide 139
Slide 139 text
Take aways
Coroutines
Deferred
Channel
A synchronisation primitive
solving the output of a stream
of values or objects from one or
several coroutine to one or
several coroutines.
Slide 140
Slide 140 text
Take aways
Coroutines
Deferred
Channel
Hot streams
Slide 141
Slide 141 text
Take aways
Coroutines
Deferred
Channel
Buffering strategies
Slide 142
Slide 142 text
Take aways
Coroutines
Deferred
Channel
Producer - Consumer
Slide 143
Slide 143 text
Take aways
Coroutines
Deferred
Channel
Actors
Slide 144
Slide 144 text
Take aways
Coroutines
Deferred
Channel
Flow
The next thing in asynchronous
programming with Kotlin that
will really give other reactive
stream solutions a match.
Slide 145
Slide 145 text
Take aways
Coroutines
Deferred
Channel
Flow
Kotlin Conf:
- ASYNCHRONOUS DATA STREAMS
WITH KOTLIN FLOW
- MIGRATING FROM RXJAVA TO FLOW
- INTRO TO CHANNELS AND FLOW
Slide 146
Slide 146 text
Thank you! Kotlin Conf:
- ASYNCHRONOUS DATA STREAMS
WITH KOTLIN FLOW
- MIGRATING FROM RXJAVA TO FLOW
- INTRO TO CHANNELS AND FLOW