- Developer's Commute
- Posts
- Do you understand Kotlin Flows? The Streamline Asynchronous Programming - Developer's Commute
Do you understand Kotlin Flows? The Streamline Asynchronous Programming - Developer's Commute
In this newsletter we will try to understand the foundation of Kotlin Flows and why do we use them.
Good Morning, It’s your captain speaking. Welcome aboard the Developer’s Commute. Today I have something special for you. In today’s section of “Code Philosophy”, we are trying to explore the foundation of Kotlin Flows.
Code Philosophy - In this newsletter, I distil what I have learned on my job. I hope you are ready to learn.
Code Philosophy
Breaking down Kotlin Flow
Introduction to Kotlin Flows
What happens when we do a network call from an API?
We receive data over the network, which takes some time to process and show on UI.
As soon as we receive the data, we can update the UI with the help of Kotlin Coroutines and Kotlin Flows.
But what is the difference between Coroutines and Flows and when to use them?
What are Kotlin Flows?
Why do we prefer Kotlin Flows?
We will explore these questions and the foundation of Kotlin Flows in this newsletter. So let’s get started.
Let's start with the basics of Kotlin Coroutines.
Kotlin Coroutines are lightweight threads that help us execute code asynchronously. It will help us produce the output of a long-running task without causing the UI to get stuck.
We use suspend functions in coroutines to produce an output of long-running tasks. Suspend functions are the types of functions that can be suspended. It means that we can stop the function for a while and resume it whenever we want. Suspend functions produce a single object as an output.
What do I mean by Single Object as an output?
If we do a network call from an API and we are waiting for a list of recipes to load on the UI. The suspend functions will call the API and return the result as soon as it receives the whole list of recipes. Instead of returning a recipe, it returns the list. So it forms a single object of the list of recipes and then returns it to the UI.
But, there is a problem. What if we have the UI of Instagram?
In Instagram, we need constant data from the server to load posts and show them to the user. If the user has slow internet then posts will take a lot of time to load. Also, if we are receiving data as a list of objects then we need to first get the list as well as map it to the UI. These problems will impact the user experience of the app.
So how do we solve these problems?
The answer is Kotlin Flows.
Kotlin flows are built on top of Kotlin coroutines, it will emit multiple values as soon as it receives it. If we take the example of Instagram UI, the flow will return posts as soon as it receives them. We can easily map them to the UI and the user will be able to scroll even if he has a slow internet.
Let's take another example, What about YouTube’s video streaming? If a user starts to watch a YouTube video, we can not wait for the whole video to load. We would want the video data to load as soon as we receive it from the server. Therefore we will use Kotlin flows.
So when do we use Kotlin Coroutines?
We primarily use Kotlin Coroutines to execute functions that are long-running or CPU-intensive. Suppose we are executing a function that needs another function’s value. If function 2 is still executing then it will stop function 1. Therefore, we need a suspended function, the function 1 will stop and resume whenever it receives the required value from function 2. The suspended functions free up the thread so that it can perform other essential tasks.
Components of Kotlin Flows
Kotlin Flow works on top of Kotlin coroutines which means it produces output asynchronously, without stopping the UI thread. Kotlin flows can produce a sequence of values with the help of its components.
Let’s try to understand the three entities of Kotlin Flows:
→ Consumer
→ Intermediary
→ Producer
→ Producer produces the output and adds it to the stream of data. The stream is asynchronous which means it will not stop the UI thread.
→ Intermediaries are used to modify each value that is emitted into the stream. We can think of it like a mapper class which maps the data from the API and maps it according to the UI.
→ Consumer consumes the values from the stream.
Repository produces the output, use cases are like intermediaries and UI is the consumer.
This is it for today's newsletter. We have explored the foundations of Kotlin flows and answered the question as to why we use them. In future posts, we will go further and explore it. Thank you for reading Developer’s Commute.
Developer Motivation
Remember, Its a marathon.