Kotlin

Kotlin Flow

INTRODUCTION OF FLOW

Flow is used for transporting stream data (Continuous data) asynchronously. It works with coroutines (Suspend functions)

There are two types of flow in Kotlin

  • Hot Flow (Channel/SharedFlow)
  • Cold Flow (StateFlow)

Hot Flow(Channel/SharedFlow):

Continuously produce data even if there is no consumer. If the consumer joins the hot flow in the middle of the emission it will receive the latest emission and lose the previous emission. Can’t think of any use case in android where we need that. If the hot flow is running and there is no consumer then there would be resource wastage.

  • INTRODUCTION OF FLOW
  • FLOW COMPONENTS
  • BASIC EXAMPLE
  • ERROR CATCHING
  • CONTEXT SWITCHING

Flow contains three main components:

  • Producer: That produces the emissions for each consumer.
  • (Optional) Intermediaries/Operators: It provides an opportunity to modify emission values before consuming them. Example: onEach, onComplete, map, buffer, etc.
  • Consumer: That collect emitted values from the producer.

Leave a comment