- 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 know what makes your app look good?
Here are some App Design trends that might help you out.
Here is everything you want to know
>>Thread<<
— Hitesh Kohli | Android Developer (@Hitesh__kohli)
5:42 AM • Sep 16, 2023
Do you want to understand API gateway?
Then you might find this tweet interesting→
Understanding the API Gateway
An API Gateway is a server that acts as the single entry point for all clients. It provides a unified interface to a set of microservices, simplifies the client-side code, and hides the internal structure of the application. Here is a detailed… twitter.com/i/web/status/1…
— Sahn Lam (@sahnlam)
5:07 PM • Nov 24, 2023
Do you want to learn about Backend development as an Android developer?
Here is something interesting for you→
Android Developer aye?
If you are an app developer, you should not only understand app development but also backend dev.
Why?
If you have no idea how the basics of the backend work.
It is hard to write code for the frontend
Here is everything you want to know:
(1/20)
— Hitesh Kohli | Android Developer (@Hitesh__kohli)
12:33 PM • Nov 18, 2023
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?