Coroutines Flow & Channels Workshop Slides

MortezaNedaei 202 views 24 slides Jun 30, 2024
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

Coroutines Flow & Channels Workshop Slides at Tapsell

YouTube:
https://www.youtube.com/watch?v=SAVRGpgIgXE

Source Code:
https://github.com/MortezaNedaei/Kotlin-Coroutines-Workshop


Slide Content

Kotlin Coroutines
Flow
Morteza Nedaei
github.com/MortezaNedaei/Kotlin-Coroutines-Workshop
linkedin.com/in/nedaei

Agenda
•History
•What is Flow?
•Stream Types
•Flow Builders
•Flow Design
•Flow Operators
•Flow Constrains
•Channels
•SharedFlow & StateFlow
•SharedFlow Design
•Combine Flows
•Flows CRUD
•Flows Lifecycle
•Convert Flows
•Practice J

History
Channel
Async
Await
Shared
Flow
State
Flow
Flow
Channel
https://arxiv.org/pdf/2211.04986https://github.com/Kotlin/kotlinx.coroutines/issues/3621

What is Flow?
“An asynchronous data stream that sequentially emits values and
completes normally or with an exception.”

Stream Types
COLD!
"
§flow
§callbackFlow
§channelFlow
§SharedFlow
§StateFlow
§Channel
LAZY
MULTICASTHOT

Flow Builders
•emptyFlow()
•flowOf()
•flow { emit() or emitAll() }
•asFlow()
•callbackFlow {}
•channelFlow {}
•MutableSharedFlow<*>()
•MutableStateFlow<*>(null)

Flow Design
Extension Function Type
Functional Interface
This block is executed
each time by calling emit
Higher Order Function

Flow Design

Flow Terminal Operators
•collect()
•collectLatest() – Conflated
•launchIn(scope) = scope.launch { flow.collect() }
•toList()
•toSet()
•first()
•single()
•fold() – both terminal and transform operator
•reduce() both terminal and transform operator

Flow Transform Operators
•transform()
•transformLatest
•onEach()
•map(), mapNotNull {}
•filter(), filterNotNull {}
•fold(), runningFold()
•scan = runningFold()
•reduce(), runningReduce()

Flow Constraints
1.Context Preservation
Flow encapsulates its own execution context and never propagates or leaks it
downstream
2.Exception Transparency
Flow implementations never catch or handle exceptions that occur in
downstream flows

Channels
Channel is a non-blocking primitive for communication between two or more coroutines
using a sender (via SendChannel) and a receiver (via ReceiveChannel).
Provide a way to transfer a stream of values while Deferred Values are used to transfer
a single value between coroutines.
similar to Java's BlockingQueue, but has suspending operations instead of blocking
ones and can be closed.

Channels

Channel Buffer Strategies
Capacity
§RENDEZVOUS (Default behavior)
§UNLIMITED (Int.MAX)
§CONFLATED (DROP_OLDEST )
§BUFFERED (Default = 64)
§SUSPEND
§DROP_OLDEST (Conflated)
§DROP_LATEST
Overflow
Strategies
Channel
example:
Gopark
Goready

SharedFlow
1.SharedFlow
2.StateFlow: Customized SharedFlow to hold latest value



Uses Lock to
manage thread
safety
NEVER
COMPLETES

StateFlow
1.Conflation – Drop Oldest: onBufferOverflow = BufferOverflow.DROP_OLDEST
2.distinctUntilChanged()
3.Replay Cache = 1: replay = 1
4.Value property: Holds the current state
It’s a SharedFlow with the following properties:

SharedFlow Design

Combine Flows
•combine()
•zip()
•merge()

Flows CRUD
Show me the code !

Flows Lifecycle
•onStart()
•onEach()
•onCompletion()

Convert Flows
•produceIn()
•shareIn()
•stateIn()

Practice :)
1.What’s the differences between Cold Flows and Hot Flows?
2.What’s the differences between Channels and SharedFlows?
3.What is the time complexity of emit function in SharedFlow ?
4.Are Coroutine Channels hot or cold?
5.What is the Logical structure of the buffer in SharedFlow?

References
•https://elizarov.medium.com/cold-flows-hot-channels-d74769805f9
•https://elizarov.medium.com/shared-flows-broadcast-channels-899b675e805c
•https://elizarov.medium.com/kotlin-flows-and-coroutines-256260fb3bdb
•https://elizarov.medium.com/execution-context-of-kotlin-flows-b8c151c9309b
•https://elizarov.medium.com/exceptions-in-kotlin-flows-b59643c940fb
•https://elizarov.medium.com/callbacks-and-kotlin-flows-2b53aa2525cf
•https://github.com/Kotlin/kotlinx.coroutines/issues/254
•https://github.com/Kotlin/kotlinx.coroutines/issues/285
•https://github.com/Kotlin/kotlinx.coroutines/issues/2680
•https://arxiv.org/pdf/2211.04986
•https://kotlinlang.org/docs/flow.html
•https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow
Elizarov papers
Papers
Kotlin docs
GitHub

Thanks for your
attention
J
Morteza Nedaei
github.com/MortezaNedaei/Kotlin-Coroutines-Workshop
linkedin.com/in/nedaei