Main

Kotlin Flows Crash Course | Hindi

Courses : https://himanshugaurandroid.in/ Github: https://github.com/himanshuGaur684/Kotlin-Flows Dive deep into the world of advanced Kotlin Flows with our comprehensive crash course! In this tutorial series, we'll explore the intricacies of SharedFlow, StateFlow, and CallbackFlow, along with essential constructs like flow{}, flowOf(), and Channel(). Kotlin Flows have revolutionized asynchronous programming, and understanding these advanced concepts is crucial for building robust and efficient reactive applications. SharedFlow and StateFlow offer powerful mechanisms for sharing and managing state across multiple components, while CallbackFlow provides a seamless interface for integrating callback-based APIs into your reactive streams. Throughout this series, we'll guide you through practical examples and in-depth explanations, helping you harness the full potential of these advanced Kotlin Flow constructs. From creating custom flows using flow{} to emitting predefined values with flowOf(), you'll learn how to design reactive pipelines tailored to your application's needs. We'll unravel the complexities of SharedFlow and StateFlow, demonstrating how to handle state updates, observe changes, and ensure consistency across your application. Additionally, we'll delve into the nuances of CallbackFlow, enabling seamless integration of asynchronous APIs and non-reactive components into your reactive flows. But that's not all! We'll also explore Channel(), a powerful concurrency primitive that extends the capabilities of Kotlin Flows, allowing for fine-grained control over data exchange and synchronization between coroutines. By the end of this crash course, you'll have a comprehensive understanding of advanced Kotlin Flows and how to leverage them effectively in your projects. Whether you're building Android apps, backend services, or multiplatform applications, mastering these advanced concepts will elevate your development skills and empower you to build reactive, responsive, and resilient applications. Join us on this exciting journey as we unravel the mysteries of SharedFlow, StateFlow, CallbackFlow, flow{}, flowOf(), and Channel() in Kotlin! Get ready to take your reactive programming skills to the next level and unlock new possibilities in your Kotlin projects. =========Best Android Development Resources in Hindi============ Complete Multi-Module Android App: https://youtu.be/rdbTu7eUYKk Paging 3 In Jetpack Compose:https://youtu.be/_dBGMdZRzGQ Side Effects in Jetpack Compose: https://youtu.be/KRqDT4oQWUU Clean Architecture in Jetpack Compose: https://youtu.be/gr_u1Czdths News App with Jetpack Compose: https://youtu.be/eIpkxJtZP2g Image Search App with Clean Architecture ( Jetpack Compose ): https://youtu.be/7yRe6bpXJV0 👍 Don't forget to like, subscribe, and hit the bell icon to stay updated with the latest content! Happy coding Instagram: /vision_android LinkedIn: https://www.linkedin.com/in/himanshu-gaur-153a43186/ Some Recommend Channel: @himanshugaur684

Vision Android Hindi

2 days ago

Yo guy welcome back to new video and in this video we are going to talk about Cutlin Floss so let's start the video so before understanding Cutlin Floss first of all you have to understand what is Floss then what is Floss here Represents an asynchronous data stream. It has two types: Hot Flows, Cold Flows, Hot Flows. We call such flows which do not matter whether someone is going to collect them or not. As you create it, it is your Real word starts the emission. If you want to see this then you
can take the example of a river because a river never stops its flow, it keeps moving continuously. If you need its water then you can take it out. Here it will not matter at all how many people draw the water, that means there can be many collectors in the hot flow, so this is called hot flow, you can understand it like if there is a tap in your house. Yes, it is tap water, when you open the tap water, you can take its water only, that means there will be only one collector, there cannot be mor
e than one collection, so in cold flow, this is exactly what happens here, which is the emission. They are not made at all, meaning you do not start the emission immediately after making them, when you start collecting, then your emission starts. We call such flows as cold flows, so here we detail hot flows and cold flows. We will see here in detail all the things that have been given to us to create it . First of all, you want a project, then you create the project and here you will need some d
ependencies, so I have included these dependencies here. If you have requirement of dependency then you can go to my github repository and get the dependency from there. So first of all you will have to create a view model here, then it is very easy to create a view model, you can simply do it here. Create V Model and extend it V Model So first of all we are going to talk about Hot Flows in Flows, so first of all we will discuss Shared Flow here, so what is Shared Flow, so Shared Flow is an inte
rface which implements If you have or use our flow interface then I will create it and show it to you, I will name it Shared Flow and to create it here you can use Moote Shared Flow. Okay, so you will see Moote Shared Flow. It has to be told that what is the asynchronous data type, that is, what is the type of data, basically, you are going to create it here in the flow, it will be an integer, okay, so here I have created a shared flow, so the shared flow would be a hot flow. It means as soon as
you make it, it will start its emission immediately. It does not matter whether you have added any collector to it or not. Okay, so I create a function here which I will name Share Flow. Testing Now because I am inside the view model, I can use the scope of the view model here and from here I will launch a routine which is our shared flow, with the help of this we will emit the data here. Okay now. What data will we emit? To emit the data, I will use repeat here. I will repeat it 10 times and w
hatever integer I get, I will simply emit it here. Emit comes with a little delay, meaning my delay will be 1 second. I can call it shared flow testing in it, let's go to my main activity tomorrow and here I will observe it, sorry, it will not be observed, it will be collected because this is flow, it is observed, it is live data, so here I simply I will use the life cycle scope so that our memory does not get leaked. If our activity is destroyed then our cotton should also be destroyed along wi
th it. Due to this, the life cycle scope gets locked, so here I will use lunch builder and its Inside basically I will use the shared flow of my view model and I will start collecting it, so I will collect the latest one here, I will use lock cat here so that I can show you the data, data is our one integer so I Given this here and I have a text view if I show you the layout here, I have a text view here in the center whose ID is main text so in this I will update our values ​​so find view by ID
. Okay, so here we are setting this integer in our text, I execute it on my device, okay, our emission here is what I am getting, this is my text view. It is being updated and here is my lock code, it is fine because I am repeating it 10 times, so here I have got data from zero to 9, so when do we use it, so I told you that you can find mutations at many places. What I mean by saying that you can share is that if I want to collect it from many places, like a river has the property that you can e
xtract any amount of water from it, you can extract it from anywhere in exactly the same way, this flow is this flow. You can consume it from anywhere like I collected it here, in the same way I can collect it by creating another collector, so this time I name it to and it will work properly in the same way, so exactly here. What will happen is that the share flow will now start collecting from two places, so one collection will be from here, second collection will be from here, okay, so if I ex
ecute this, okay, then you can see here, shared and shared to mean. Now it has two collectors and it is being collected from both the places and our app is not crashing, so we use share flow by brute force when we have to collect a flow from multiple places, mainly we use it. You can use things to broadcast because there can be many listeners to the broadcast, there can be many collectors and they will be listening to it. If some operation has to be performed on the basis of whatever event will
happen, then you can do it very easily. You can do this and that's why we use mute shared flow or shared flow. Shared flow is a hot flow, which means there can be more than one collection. If you check the constructor of mute shared flow, then You will see three to four things here: Replay Extra Buffer Capacity on Buffer overflow, so these are three things which we get in its constructor, the first parameter which is Replay, I show you how to use it here, so I have shown it here. But replay sent
four, if we add a new collector in it, then we will get the four values ​​of the values ​​emitted in the last one. Okay, now let me show you this, if you will not understand, then the second value I have here. This one is where I can produce delay and delay, I will give it for 4 seconds, okay, I will go back to the lock cut, so look here, my second flow is still not attached in it, okay, and basically here, our second flow. If there is a flow, it is attached in it, so the last three values ​​ar
e 0 1 2, because till now 01 2 means only three values ​​have been emitted in it, so I got three values ​​here, my reply was how much was four, but now the value is only three. If it has been emitted then how will it give four, so here I got three values, after that the normal flow which we were trying to collect is getting it here like three got four, five was got, okay, so what I mean to say is that this is the use of replay. You will do this when you have to give the last values ​​to our new
collector, then now let us talk about its second parameter which is extra buffer capacity, so extra buffer capacity, you can know from the name that which is your Apart from the buffer created, how much buffer capacity do you want to give? I want to give four here and if you pass four here, then whatever will be your buffer, suppose you get a buffer of two in default, then now your buffer will be whatever. That will be a buffer of 2 × 4 ×, okay, after this you have on buffer overflow. Buffer ove
rflow means that let's say I have given a buffer of six. Okay, now I have seven values, so my buffer should be full. So what will you do in that case, then you can tell the same thing here. Buffer overflow drop oldest. Drop oldest means that you wipe out the things which are kept in your buffer and you wipe out the new stuff which is coming. Keep it in the buffer, this is your drop oldest and similarly we have drop latest and yes drop latest, this means that the new value which is coming to you
from the flow, you drop only that which is in the buffer. If you don't do anything to it, that's why we use drop latest overflow and lastly we have suspend, so suspend means that if your buffer is full, you have data coming, then the data is coming right. Suspend it and keep it suspended until your buffer gets cleaned and as soon as your buffer gets cleaned, resume what you had suspended and bring that data and keep it in your buffer . Okay, so in case of suspend, your data will never be lost be
cause it will down its up stream and when needed, it will up and restore the data again. Okay, so these three things happen. One more thing I want to tell you in the recording share flow of our buffer overflow is that if I execute this app and rotate my device, okay, so here you There will be no visible difference, okay, whatever our collection is, it will continue to run continuously, it will not make any difference that you have converted your portrait mode to landscape, it will not make any d
ifference, okay, so what I mean to say is this. It has nothing to do with your life cycle, it has nothing to do with your orientation, it means it is independent from everything, it has nothing to do with anyone, its meaning is as if you created it, it started its mission, after that it is your responsibility. How do you handle it, there is nothing to manage the state in it, you can't do anything basically in it, it will just start emitting the data, that's it, apart from this it will not do any
thing, okay now we Let's talk about state flow basically, so what is state flow? So we use state flow mainly to manage our state. Now the name tells us that state is what state is and how we use it. If people look here, then for this we will create a variable here and name it state flow and will name it from brute force state flow and in brute force state flow, you have to send the initial value. If it is a must, then I will send it here as zero, okay. And in exactly the same way, I will create
another function for my state flow, now I will call it here state flow testing and will do this thing, I will comment, I will go to my main activity and here I will copy and paste this life cycle and this I will comment out, so I have removed the delay here and now I will execute it and go to lock cut, so here my counting has started from zero. If I rotate it, you will notice that I have rotated it and this It was rotated for the fourth time, okay, but when I rotated it, it gave me back the last
emission I had, as you can see here, when I rotated it, it gave me four in landscape also, which means the state. It saved it and gave me that state in landscape, so basically if you do this screen orientation, you do not want your state to get lost, then it prevented the state and that is why we call it People use view models to hold their state. In view models, the state flow is a hot flow, but here the collectors who collect it can be one and cannot be more than one. If you have If you have
attached this then your app will crash, so I have this state flow and I want to convert it into shared flow, is it possible? Yes, it is possible and that's why we use share in here. Okay, so what is the requirement of sharing, if you press control here, you will get to see the scope in which you want to launch it, so I want to launch it in my view model scope, second parameter is my started then started. By using this you can tell this flow when you start your emission, for that I have many opti
ons here like sharing started egirl now you will know the meaning of egirl as soon as I make this you Immediately start the emission, it is two seconds lazily, this one lazily means you do not emit it until I apply the collection, that means lazy, like we use lazy, if we treat any variable, then that is till Initially it doesn't happen until we access it for the first time, okay and lastly here, while subscribed, okay, while subscribed means you don't do it until someone subscribes, that means d
on't start the emission, okay. Lastly here is the replay replay what I told you replay how are the buffers which we have for collection, if there are many collectors to do that collection then how many values ​​of last do you want to give to the new collector who will be created . For this, reply is used, reply is used, so I pass one here, now this is our mute state flow, it has become a mute shared flow, so the behavior of shared flow is the same behavior, now you get to see it. What I mean to
say is that with its help you can convert any state flow into a shared flow and in exactly the same way if you want to convert any shared flow. If you have to convert it into state flow then you will use state in here, simply you will send its scope here and press control here, second started which I told you when you want to start sharing and lastly. You have the initial value, what will be the initial value, so if I pass zero here, then this will be my initial value because we have to mention
the initial value of every state flow, that is why sharing is possible. When do you want to start, you can pass that here while subscribe, then in this way you can convert any shared flow into state flow, okay then I remove it from here, I have told you the method here how. You can put share flow into state flow and state flow into share flow. So let's start with channel flows. So for that I will create a channel flow here. So to do that you have to click here. Simply channel and whatever data t
ype you want to use here, I want to use int here, so I will pass int from this, you simply import it and here you will see three parameters: Capacity on buffer overflow. So I have already told you about on buffer overflow, on undelivered element, this is a new thing, we will discuss it now, so first of all the capacity means, if you want to know what is the capacity of this channel, then I will give it here. I want and its on buffer flow is buffer overflow dot suspend OK and finally we have on u
ndelivered element. This is a Lada, so inside this, whatever element will not be delivered to me through this channel, I will get it here, okay. So in a way, this is a fail event, you can understand it, so I will put a log D for it here, okay so this has become our channel, now I have to create a function for this, so I will simply copy paste it and channel. I will replace the state flow with my channel and here there is no emission, we send things in the channel, so here we will simply send thi
s integer value and here I change its name, cool. Now I will call this in my it block channel flow testing and to collect this channel in my main activity I will create another variable, I will name it m channel and here I will use this channel and consume it as a Flow : You can consume it in two ways, you can consume it by making it a cold flow , you can consume it by making it a hot flow, so this is what I have used, it will create this channel, we will collect it as cold flow. You can do this
, if you want to convert it as hot flow, then you can use receive as flow here, then I will use consume as flow here, so I will collect this now in my main activity, so I will simply use this. I copy this which I had used earlier and simply here I will use M channel's OK, I comment here and I simply execute this thing, so basically in this way I am collecting that channel. Now I will execute this on my emle ok so here it has been run I will check the lock cut so you can see the emission proper i
s happening here 1 2 3 4 5 6 7 8 9 10 cool to ro to Till Nan means 10 elements. Now I will go back to my view model and remove the delay that I was producing here and execute it. Okay, so now you will see that here we have received all the elements 0 1 2 3 4 5 Because there was no delay here, we collected all our emissions in one go. Okay, now here I suspended our buffer overflow , which means if we have more things than our capacity. If you get it then you suspend whatever up stream data is the
re and when our buffer gets exhausted i.e. we will clear it, then you resume that suspend function and take the data from there, that is why you will get nine in the email here. You will be able to see, what if I use drop oldest, which means you remove the oldest value in the buffer, now if I execute this, you will see some changes, so here you will notice that we have Delivered element is from zero to seven, meaning the last two are the ones we have got. The oldest value has not been delivered
to us yet. Why is it so? Look at the buffer, the capacity of that buffer is two, so the first is zero. The forest has come, it is okay, buffer buffer, we are full now. If three comes, then basically it will drop the oldest value, meaning what is the oldest value, so if zero is dropped then two comes, then three comes, four comes five, six comes seven, okay, eight comes, so what happens when eight comes? If our six is ​​removed then what will we have in the buffer, seven and eight and like nine c
omes then eight will come next and if our seven is removed then 89 which will be there will be ultimately received by us while collecting, you can see here. If you are getting then finally we have got two elements which were our oldest value, it has discarded them, basically it is saying that brother, this is an undelivered element, okay so this is your buffer overflow drop oldest, this is your practical example etc. If I drop the latest one here , what will happen, you guess what will be the an
swer, I will execute it, this time the value you have got is 0 and so on, it is obvious that you are saying that the latest one will come. Drop it, what is the latest value here? 01 is fine, 0 and what is 01, we would have got it first, now after this, whatever we get will be our new values ​​and I have told the new values ​​that brother, drop it to us, we need it. If it is not there, then it kept 01 with itself and discarded all the other values ​​and that is why you would be seeing one here. W
hat happens in the case of suspend? In the case of suspend, we now have steam data from which We take the values, if our buffer gets filled then it suspends that stream itself and after clearing our buffer again, it resumes that suspended thing i.e. suspended which will be our stream data and Again fills the buffer, that is why in the case of Suspend, you were getting to see nine here. In the case of Drop Oldest, you were getting to see nine here because the things from row to seven have been dr
opped. The last two The elements which are latest were in the buffer, that is why you were seeing nine here in the oldest case, but now I am saying here that drop only the latest, that means only our oldest will be left, so what will be the oldest first? One row will be emitted, then one will be one, then one row will remain in the buffer, what will be the rest, all will be discarded, that is why you are seeing one here in the third case, I hope you are clear about everything here, one more thin
g here I have If we have used consume flow, then consumers flow is cold flow. Here we cannot put more than one collection on it, that means there cannot be more than one collector. You can see its practical example here, I will rotate it, so like I have done in my Our app crashed when we rotated the screen. This happened because M channel is a cold flow and there cannot be more than one collector on a cold flow. If you look at the message that you are getting in crashlytics, then this will be th
e received channel. Consumers flow can be collected just once but what we are doing is like we are rotating the screen on the gut again another collector is being collected on this particular flow due to which you can see the crash here. So, I had told you about this thing from the beginning that this problem will arise if you install more than one collector with cold flow and if you add receive flow here then it has become hot flow and Now you execute this and here now if you rotate it, you wil
l not see any error. Okay, so as I told you, now what has become of hot flow, so you will not see any error. Okay. Here is about the channel, where do we use the channel, how do we use it, I told you about its parameters, I told you about the buffer overflow, here I told you practically what actually happens if we drop the latest here. Use drop oldest or use suspend. Now the last flow we have is callback flow so when we use callback flow then see if you have used fire base or you have used somet
hing in which we People are using add on success liner, if they are using add on failure liner, then you would have the idea that we were not able to use it that well with flows and to convert it into flow, callback was given to flow. If you are an ad developer and want to improve your skills then you can visit my courses. In the description you will find the first link to my website. For that, first of all you will have to go to the build gtl here and add this dependency. Here you will get the
dependency of Firebase Authentication. Okay, using this I will explain to you when you will use the call back flow. Let's end in our main view model. What I will do here is that I want to write a function which will return me the flow and ultimately it will also authenticate me with our af base, so to do that I will create a function here with the name af base auth and here this Inside the function, I will use f base auth, I will create an instance of this and now I am going to sign in here with
my email and password, so the email password, I send the MT here. Now you will notice here, I have here. Add on success liner, add on failure liner and we have add on cancel liner here. Okay, so we have three listeners here. So inside this leda expression, you notice that we are getting the authentication result, but here you can use flow. You cannot do this because to use Flow you require routine scope and we do not have any scope right now. Now in such a case if you want to use Flow then call
back Flow has been created for that, see the name. It is clear from this that Call Back Flow means Call Back wherever you are. If you do not want to convert into flow, then you can use Call Back Flow. To do this, you will have to put equal to here. It is on you. Do you put it or not? You convert it into call back flow and here you tell what will be the result, basically what will be the type in which you will get the flow, so I say simply string here, it is ok, so it is ready. Call Back Flow: Th
ere is a function inside the call back flow called tryset, it does not need any cotton scope. Now whatever you send here, this is ultimately the call back flow key, we can collect it in any activity. Or see how to do it in any fragment, if I have to send it here then I will send it here as success, success is fine, just like you used to use emit, here we will use trysend, the only difference is this. That tris send does not require any type of cotton scope and here since it is failed then I will
send here as failure and if it is canceled then I will send here as canceled. Okay, so now this is your complete call back. This is convert. It is done in a flow, as the flow acts, you can use it also, you can collect it like normal, we play with flows and lastly, if you want to close something, then you use Await Close here. And if you have to free the memory after the execution, then you can do it there. Okay, now how will you use it? So go to the main activity, simply copy it and paste it he
re and in the view model, use Fire Base. That's okay, now you can collect it and when you collect, you will receive the value here which you will be trying to send from here. Okay, so you can use it in this way, by the way which is called. There is back flow, I think it is cold flow, so you cannot do more than one collection in it , so here we will talk about some basic flow builders using which you can build the flow, here you create a variable. The name Flow is correct, here you can use Flow B
uilder, so in this Flow Builder, first of all you have to tell about what you are making flow here, so I am making a string here and inside it you can emit. If you can then you will use emit here and you can do whatever you want to emit here, okay this is your cold flow, this is cold flow and in the same way if you want to create more flow then Here we have flow off then flow off flow off so here you can create any flow here if you want to put a string then you can send the string this is basica
lly cold flow these two will be ours cold flow that Means there can be only one collector, this is a very basic thing, if you use use cases, then generally I use this flow builder to put my use cases into flow, so these are some of our basic flow builders. Which we use to convert any stream into flow. See you in the next video where we will discuss some more things. Till then bye.

Comments

@abhijeetmule1521

very good explanation 👌

@raushansingh003

Thanks bro keep uploading such content 😊 these are very helpful to learn core of android development