Coroutines in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to my channel in this video we will learn about Co routines I know Co routines is a very New Concept for you but it is very easy we will understand the basics of Coe routines through an example letter okay first let's see what is co routines Co routines are a concurrency design pattern that allow for sequen execution of task in a non-blocking manner in very simple language Co routines manages multitasking very well by dividing its processes into multiple threads now what is thread thread is like a pipeline where all the processes take place processes as in UI interaction image loading file downloading and all so this is our main threade here all the processes take place now imagine how tough it is to handle so many processes by a single threade the app will lag it will freeze even it can get crashed and that's not how professional app should work right here core routines was introduced to overcome the burden that was put on Main threat but how will it overcome by dividing the processes into multiple threade you can call it co routine threade or background threade now as you can see we have two threade one is main threade that is handling UI interaction logical operations and arithmetic operations and then there is a background thread that is handling file downloading and image loading the process are divided into two threads which makes Co routin non-blocking because they allow for sequential execution of task without blocking the main thread that ultimately improves app performance and prevent lagging of freezing great right now in core routines there are few terms that you need to understand for which first we need to see how do we create corins here it is to imp Implement Coe routines in the app you need to add this dependency then this is a general syntax Co routine scope dispatcher doo. launch to understand code routines deeply we need to understand these two TP scope and its type and dispatch and its type so first let's understand what is scope scope basically determines the lifetime and behavior of Coe routines by executing asynchronous code now what is asynchronous asynchronous in Coe routines means that the task can be executed at the same time without blocking the main thread got it so we have two types of scope first is global scope and second is co routine scope you can use any scope based on your requirement first let's see what is global scope Global scope is not tied to a specific core routine it allows task to be executed concurrently means at the same time without being limited by the life cycle of individual Co routines while Co routine scope is TI to activity life cycle or fragment life cycle means consider core routines is running and suddenly the app is destroyed means remove from the recent app list then in that case cor routines will also be destroyed because obviously it was dependent on life cycle but if you have used Global scope then core routines won't be destroyed because obviously it was not dependent on life cycle easy right then let's see what is dispatchers very simple definition dispatcher defines which thread to use for executing the code like main thread or background thread that we need to Define okay there are three types of dispatcher let's see each one of them first is dispatcher default second is dispatcher IO and third is dispatcher main basically which dispatcher are we supposed to use for which process like for CPU based operations such as Computing or sorting algorithm then we are supposed to use dispatcher default then for Io based operations such as Network request or file operations we are supposed to use dispatcher IO that is our background threat then for all the UI related operations like set on click listener button or set tag we are supposed to use dispatcher main that is our main thread itself got it wait come back to the previous slide look here main thread is handling all the UI related and mathematical operations and background thread is handling all the io related operations like file downloading and image downloading right see default thread is main thread only which is pretty good at handling UI related operations such as set on click listener and all but if you tell main thre to handle file downloading as well then it will be like I can do it tell dispatcher iio or background threade to handle it now let's understand it better with a simple code routine example project okay open Android Studio here is an example project that I created to show you the difference between a app without Co routine and a app with Co routines first let me give you a quick overview this is where we have added the view binding then come to activity main.xml here we have created two processes in the same screen first is counter process which we have to manually update the count by clicking on the start counting button and then second process is where a file downloading will be done of course there is no actual file getting downloaded it's just a demo okay so to create this UI first I have used constraint layout as a parent layout then text view representing process one then another text view which represents the count whose ID is Count number then a button whose ID is Count button then another text view for process two and then a button whose ID is download button got it now come to main activity I have not used score routines yet see we have implemented binding then kept initial counter value as zero then first button is Count button button the logic says when you will click on the count button the count number that is the text view will be incremented by one every time then we have another button which is download button as I said it's not going to download an actual file but it's just a representation of how a file is downloaded like I have used a for Loop whose variable is I that starts from one and goes up to like La then inside it there is a loog tag this loog tag will be visible in lock cat so it goes like we have used tag as a representation for log then inside it a message that says downloading I that will be the number going from one to L in which threade like threade do current threade name represents the threade name in which all this process is happening either it will be main threade or in background threade it will just display the current threade name that's it simple right now remember what I said while explaining it default thread is main thread that handles all the UI related operations such as set on click listener it will handle it wisely but that is a file downloading process as well that too has to be handled by the main thread but do you think it will be able to handle it let's find out by running the app now see I'll open lock cat so you can see the fake downloading process this is old process so I'll quickly clear it I'll use Tag as a reference then I'll click on start downloading button see how fast it is getting downloaded on the main threade but in the middle of the process I'll start another process as well that is start counting and look it is lagging it is freezing and finally got crashed it was tough for main three to handle two different process at the same time in the same trade hence we will use code routines let's modify the code so go to build. gradel here in dependency add the following Coe routine dependency you can find it in the description box then click on sync now and done we want modify activity main code so directly come to main activity here set on click listener is handled by main thread fair enough but we don't want file downloading to be handled by main thread instead who should handle it of course code routine or background threade so how do we create code routines first is to decide which scope remember Global scope or Co routine scope I'm fine using Co routine scope even if it is dependent on the life cycle so I'll write here as Cod routin scope then I have to decide which dispatcher are we supposed to use as I'm performing file downloading process which obviously means I need to use dispatcher IO so I'll quickly write here and then finally launch will execute the below code so I'll cut paste it and done now this process will run in Main threade and this process will run in the bankr threade easy right let's run it look I'll click on start downloading and see here it says the downloading is running on the background thread simultaneously I'll click on start counting Button as well which is running on the main thread look no lagging no freezing and no crash as both the processes are in different thread hence it is working perfectly fine great right so that's it this was all the basics of Coe routines later in future I'll cover Co routines advanced concepts as well okay also for more updates you can follow us on Instagram or join our telegram group Link in the description box so yeah that is it for the video if you're new to this channel then please consider subscribing to my channel and I'll see in the next [Music] video
Info
Channel: Android Knowledge
Views: 1,217
Rating: undefined out of 5
Keywords: coroutines, coroutinescope, how to use coroutines in android, coroutine, android, kotlin coroutines, kotlin coroutines 101, how to test coroutines, what is a coroutine, what can you use kotlin coroutines for, what are kotlin coroutines, android devs, how to use kotlin coroutines, android talks, kotlin 101, what is a thread, how to use kotlin, thread, android developers, asynchronous, kotlin, coding, suspend, tutorial, tutorials, android conference talk, programming, concurrency, knowledge
Id: onnnefZCgmQ
Channel Id: undefined
Length: 10min 15sec (615 seconds)
Published: Tue Oct 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.