• Developer's Commute
  • Posts
  • Do you understand Kotlin Flows?- Shared Flow vs State Flow - Developer Commute

Do you understand Kotlin Flows?- Shared Flow vs State Flow - Developer Commute

In this newsletter we will explore more about Kotlin Flows, some interesting tweets, articles, memes 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, memes 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.

APP FACTS

Kotlin Flows Part -4

In this newsletter, we will explore more about Kotlin Flows. Previously we have learned about the basics and its parts.

If you haven’t read the previous newsletters, here are the links:

Let's explore some important parts of Kotlin flows. These parts are important not only for making you a better app developer but also they are frequently asked in coding interviews.

What are Shared Flows and State Flows?

What is the difference between the two?

The Basics

StateFlow

State Flow is just like any other flow. It sends a flow of data that can be updated in the UI. But, there is a difference between flows and State flow. Flows are cold, but State flow is hot. It means that State flow will keep on sending the data even if there is no collector present to collect it. It works just like channels would. Thus it is fit for the UI that needs to be updated constantly.

Here is an example:

In this example, the ViewModel takes the repository inside a constructor. A private val UIstate is updated every time the repository receives the data successfully. It is initially an empty list but it gets updated inside of the init block. A uiState variable is created which is not private so that we can share it with the UI. Here we are using Mutable State Flow which suggests that it is a mutable type of State Flow that receives the data and stores it into another variable of type State Flow. Then State Flow is shared in the UI to update the data in the UI.

An important fact to keep in mind is that Stateflow retains its last send state. It means that it will store its last send value. If a collector is attached, it will receive the last send value and then it will receive further values.

SharedFlow

Shared flow works similarly to state flow. It is a hot flow of data which means that it keeps on emitting values even if there is no collector attached. It can be used to update for example user preferences based on what the user is consuming on your platform.

There is a difference between state flow and Shared Flow. State Flow retains its last sent data whereas Shared Flow does not. It means that if a collector is attached to a State flow, it will receive its last sent data first and then the subsequent ones. Whereas, in Shared Flow the collector will only receive the future data.

Here is an example of Shared Flow:

In the above example, the tick handler class emits a time interval in the form of shared flow. The news repository takes the tick handler class in the constructor and we can collect it from the Tick flow variable. It calls the refresh Lastest news function which refreshes the latest news after some time.

An important fact to keep in mind is that we can use a replay operator which saves the latest data. We can specify how much data the replay operator needs to remember by passing a specific number to the operator.

This is for today I hope enjoyed this newsletter. If you enjoyed it please let me know and if I have written something wrong please forgive me.

Thank you for reading

Regards,

Developer Commute’s Captain

Free Ebook

Developer Meme

Which one are you?

Tweets That You Might Love

Exploring Interesting Tweets

Tweets That You Will Find Interesting


Do you want to make your app look good?

Here is an interesting tweet for you →

Do you want to understand API gateway?

Then you might find this tweet interesting→

Do you want to learn about Backend development as an Android developer?

Here is something interesting for you→

ArticlesThat You Might Love

Exploring Interesting Articles

Articles That You Will Find Interesting


Build Video Calling for Your Android WhatsApp Clone With Jetpack Compose:

Here is an interesting article that you might want to check out →

Top 10 Android Memory Leak Causes:

Here is an interesting article for you →

Deep Dive Into Android Memory:

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