KotBreaking down Kotlin Flows Part :3 — Developer’s Commute Newsletter
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
— — —
Do you want these topics in your Email Inbox:
Interesting facts about apps
Expert Advice
Core Concepts
Developer Stories
Apps Reviews
Human error
Code Philosophy
Additional:
- Coding Memes
- Interesting Tweets & Articles
- Motivation
— — —
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
— — —
Do you want these topics in your Email Inbox:
Interesting facts about apps
Expert Advice
Core Concepts
Developer Stories
Apps Reviews
Human error
Code Philosophy
Additional:
- Coding Memes
- Interesting Tweets & Articles
- Motivation
— — —