- Developer's Commute
- Posts
- Become a better Android Developer with Kotlin Flows - Developer's Commute
Become a better Android Developer with Kotlin Flows - Developer's Commute
Here is the part 3 of Kotlin flows. We also have memes, free Ebook, interesting tweets and articles
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 -3
Kotlin Flows Part -3
In my previous newsletter, we explored the foundation of Kotlin Flows. We learned about Kotlin Flowbuilder API and how can we use it to call a simple API call and receive the stream of response.
In this newsletter, we will explore Kotlin Flows further.
Modifying Kotlin Flows:
Let's start by exploring how can we modify Kotlin Flows.
We have previously seen that there are three parts of Kotlin Flows. Three parts are:
→ Producer
→ Intermediary
→ Consumer
Exploring Intermediaries
We can modify the stream of data or flow of data using the intermediary. The intermediary can be used to modify the stream of values without collecting them. We can modify the stream of data with a chain of operators like the .map function.
Kotlin Flow Operators
Let me explain with this example:
In this case, the class NewsRepository requires the injections of NewsRemoteDataSource and UserData.
We are saving a flow of a List of news articles which comes from the news remote data source.
We have attached a .map function to the flow of the list of articles which helps in filtering the articles according to the user’s choice (favorite topic)
.onEach function helps us save the articles to the cache
These intermediaries do not trigger the flow of data instead it just modifies the stream of data that is present at the current moment.
Exploring Collectors
In order to get the flow of data we need to collect it. Kotlin flows are cold in nature therefore they will not produce any output until they are collected.
We can use the .collect function to collect the stream of data. The collect function is a suspended function therefore it needs to be triggered in a coroutine scope.
Here is an example:
Kotlin Flow Collector
In the latest news view model we call the news repositories function favouriteLatestNews and add a collect function to it.
We can use this collect function and show it on the UI using the Lazy column in Jetpack compose. We can notice that the function is called under viewModelScope which is a coroutine scope.
Important Note for Kotlin Flows:
Kotlin flows collect the flow of data from the same coroutine context of the emitter. It means that the coroutine context of the flow of data will be the same as that of the collector. But if we try to change the coroutine context of the emitter to a different context then the collector would not work.
Let's take an example, if a function is emitting values in the form of Kotlin flows on the Main thread then we can only collect on the Main thread. But suppose we have an API call we can not call it on the main thread, we would need to change the context of the flow. Therefore to solve this problem we use the flowon function. The flow on function changes the upper stream of the flow of data. It means the context of the emitter changes but the context of the collector remains the same.
Kotlin Flow Flow on Operator
his example, the flowOn operator is used to change the coroutine context of data to the default Dispatcher.
Converting the API call to a flow of output
If we want to convert the API-based to a flow we need to add the callbackFlow function to it.
Here is an example:
Kotlin Flow Call Back Flow Operator
In this class, we are trying to get the data stored inside of the document “app” which is stored inside Firebase Firestore. If we want to return an output we cannot directly write the return function.
And if we want to constantly update our data from Firebase Firestore we can not just use coroutines instead we have to use the Kotlin flows.
So if we want to collect the Flow of user events from Firebase Firestore we will use the callbackFlow call which converts the output of this function to a flow of data.
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
Tweets That You Might Love
Exploring Interesting Tweets
Tweets That You Will Find Interesting
Do you want to learn about JWT Authentication?
Here is an interesting tweet for you →
Android developer aye?
Have you heard about JWT token?
If yes, you must have used it in Authentication of your app.
If no, we are going to explore it in this series of thread.
Here is everything:
(1/)
— Hitesh Kohli | Android Developer (@Hitesh__kohli)
4:06 AM • Nov 21, 2023
Do you want to learn about JWT and Sessions?
Then you might find this tweet interesting→
Android Developer aye?
Do you understand what JWT tokens are?
It is an essential part of Authorization and it is important in order understand the basics of auth in app development.
Here is everything you want to know:
(1/21)
— Hitesh Kohli | Android Developer (@Hitesh__kohli)
5:08 AM • Nov 20, 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
Single Activity vs Multiple Activities Architecture:
Here is an interesting article that you might want to check out →
Do you want to know what are Kotlin Coroutines?
Here is an interesting article for you →
Why do you think users like apps over websites?
Here is an interesting article for you →
Developer Motivation
What do you think?