Core Concepts

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In your journey through Kotlin Flow, it’s crucial to understand its three main components: producer, intermediaries, and consumer. Each component plays a pivotal role in handling data within your apps. In the previous lesson, you learned that Kotlin Flow revolves around data streams. Now, you’ll break down these components one by one from that perspective.

Producer

You’ll start at the source of the data stream. The producer is the starting point of a Flow. It generates the data that flows through the system. This can be static data, dynamically generated values, or responses from network requests, among others. Kotlin provides several builder functions to create Flows, such as flow, asFlow, flowOf, and callbackFlow. You’ll learn more about them in the following lesson.

Intermediaries

Intermediaries in Kotlin Flow are operations or transformations that data undergoes as it flows from the producer to the consumer. These are the functions that modify, filter, or transform the data stream in any way necessary before it reaches its final destination. Common operations include map, filter, take, and many more. These are crucial for preparing data as per the consumer’s requirements.

Consumer

The consumer is the final point of your Kotlin Flow. It’s where the data stream completes its journey, being utilized or observed. In practical terms, the consumer is often a function or block of code designed to react to the data it receives. This could be updating the UI, storing data in a database, or even triggering other processes. The most common function for a consumer to interact with a Flow is through the collect function.

Putting It All Together

Now, you’ll tie all these components together in a single story. Look at your previous example, but with your new knowledge about producer, intermediaries, and consumer:

class ViewModel {

  // ...

  private fun subscribeToOrders() {
    // Producer
    carrotOrders()
      // Intermediary #1
      .onEach { showLoading() }

      // Intermediary #2
      .flatMap { order -> carrotStore.fetchCarrots() }

      // Intermediary #3
      .filter { carrots -> matchesQuality(carrots) }

      // Intermediary #4
      .map { carrots -> carrotCutter.cutCarrots(carrots) }

      // Intermediary #5
      .map { cutCarrots -> carrotCooker.cookCarrots(cutCarrots) }

      // Consumer
      .collect { cookedCarrots -> 
        hideLoading()
        preparedMeals.emit(cookedCarrots)
      }
  }
}
See forum comments
Download course materials from Github
Previous: Introduction Next: Kotlin Flow Builders