• Developer's Commute
  • Posts
  • Do you understand Kotlin Flows Part 2 - Developer's Commute - Code Philosophy

Do you understand Kotlin Flows Part 2 - Developer's Commute - Code Philosophy

In this newsletter we will explore more about Kotlin Flows. It will also include tweets, articles and coding motivation.

Good Morning, It’s your captain speaking. Welcome aboard the Developer’s Commute. Today we will explore more about Kotlin Flows, some interesting tweets, articles and in the end, Coding Motivation to keep you moving We hope that you enjoy this ride with us and you have a pleasant journey ahead.

Code Philosophy

Breaking Down Kotlin Flows Part -2


Welcome to part 2 of Kotlin flows. Previously we have understood the basic foundations of Kotlin Flows. In this newsletter, we will first summarize and move further with Kotlin Flows.

Summary of Previous Newsletter

Kotlin Flows work on top of Kotlin Coroutines and they provide us with a continuous stream of data. Kotlin Coroutines return a single object so they can not replace Kotlin flows. Each has their own purpose. The best example of the usage of Kotlin Flows is video Streaming. Kotlin Flows can be divided into three parts: Producer, Intermediary and consumer.

Now Moving on with Kotlin Flows.

We have previously explored that we needed a continuous stream of data to stream video properly.

There are two ways to give a continuous stream of data:

→ Using Channel

→ Using Flows

What is the difference between Channels and Flows?

→ Channels are the Hot flow of data

→ Flows are the Cold flow of data

What is the difference between Hot flow and Cold flow?

→ Cold Flow works only if the data is collected

→ Hot flow works even if the data is not collected

Let me explain the difference between Hot and Cold flow. Hot flow is the flow of data stream that needs not be collected. It is just like a radio station, it will keep on playing even if nobody is listening to it. But cold flow is the flow of data stream that needs to be collected. It is like a YouTube video, it will only start playing when the user wants it to.

Hot flow will continuously produce data even if no one is trying to collect it. Hot flow can be used in the case of Google Maps. Even if we are not collecting the location, it needs to be updated every time.

Cold flow will not produce data if no one is trying to collect it. Cold flow can be used in the case of video streaming.

One major disadvantage of Hot flow is that it leads to wastage of resources and data. Since it keeps emitting data even if nobody is receiving it, the data is wasted and never retrieved. Also, resources that are necessary to emit the data are being wasted continuously. Another flaw of Hot flow is that it needs to be stopped manually whereas the cold flow stops automatically if the collector is removed.

Here are the examples of channels and flows:

Example of Channels

In the output, we will only log → 2 and 4 which we will not be able to receive 8. It means that it has been produced but not collected

Example of Flow

In the output, we will be able to log all the output values even after the delay because flows only work if some collector is collecting them.

In this newsletter, we are only talking about Kotlin flows.

Let's explore Kotlin flows further and try to break down the code.

To create flows we use FlowBuilder API. It helps us create a flow of stream of data which can be emitted by using the emit function and it can be collected by using the collect function.

Explaining Kotlin Flow with API call

In this code, the NewsApi interface has a suspend function which will be used to request the List of Articles from the server. In the class NewsRemoteDataSource we inject NewsApi and declare a refresh variable for the flow API.

Inside the class, the latest News variable expects the response of the Flow of a List of Article headlines. It is equated to the flow builder API. Inside the Flow builder, we receive the news articles in the latest news variable. News API is called and it fetches the latest news from the API. After fetching it emits the news at a delay of refresh intervals. We have declared a delay so that we can receive the output from the suspend function.

This is it for this it for today. I hope it made Kotlin flow clear to you and you enjoyed reading it.

In the next few newsletters, we will explore them further.

If I have written something wrong please forgive me. Thank you.

Regards,

Your Captain

Tweets That You Might Love

Exploring Interesting Tweets

Tweets That You Will Find Interesting


Do you want to understand the basics of Kotlin Flows?

Here is an interesting tweet for you →

Do you want to learn about different Architectural patterns?

Then you might find this tweet interesting→

Do you want to understand Ktor?

Here is something interesting for you→

ArticlesThat You Might Love

Exploring Interesting Articles

Articles That You Will Find Interesting


Do you want to understand Kotlin flows?

Here is our Newsletter that you might want to check out →

Do you want to understand Why users prefer Apps over websites?

Here is an interesting article for you →

Do you want to understand Clean Architecture?

Here is an interesting article for you →

Developer Motivation

What do you think?

Goodbye Fellow Developer

Over and Out Passenger. I hope you enjoyed this journey

I hope you enjoyed this Developer’s Commute. Thank you for joining me.

Your Captain- Hitesh Kohli