Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Coroutines Flow & Channels Workshop Slides

Kotlin Coroutines Flow & Channels Workshop Slides

Kotlin Coroutines Flow & Channels Workshop at Tapsell

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

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

Avatar for Morteza Nedaei

Morteza Nedaei

June 30, 2024
Tweet

Other Decks in Programming

Transcript

  1. 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
  2. History Channel Async Await Shared Flow State Flow Flow Channel

    https://arxiv.org/pdf/2211.04986 https://github.com/Kotlin/kotlinx.coroutines/issues/3621
  3. What is Flow? “An asynchronous data stream that sequentially emits

    values and completes normally or with an exception.”
  4. Stream Types COLD 🥶 🥵 § flow § callbackFlow §

    channelFlow § SharedFlow § StateFlow § Channel LAZY MULTICAST HOT
  5. Flow Builders • emptyFlow() • flowOf() • flow { emit()

    or emitAll() } • asFlow() • callbackFlow {} • channelFlow {} • MutableSharedFlow<*>() • MutableStateFlow<*>(null)
  6. Flow Design Extension Function Type Functional Interface This block is

    executed each time by calling emit Higher Order Function
  7. 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
  8. Flow Transform Operators • transform() • transformLatest • onEach() •

    map(), mapNotNull {} • filter(), filterNotNull {} • fold(), runningFold() • scan = runningFold() • reduce(), runningReduce()
  9. 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
  10. 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.
  11. 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
  12. SharedFlow 1. SharedFlow 2. StateFlow: Customized SharedFlow to hold latest

    value Uses Lock to manage thread safety NEVER COMPLETES
  13. 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:
  14. 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?
  15. 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