Coroutine Scopes - Kotlin Coroutines

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there and uh welcome back to my new video in this video i'm going to explain you more about uh corrupting scopes what they are and how they actually work so if you haven't watched my previous video about core routine basics then i highly suggest you to do that before you continue watching this one because there i have explained everything you need to know before you start working with core routines okay so the first thing you need to know is that every core routine needs to run inside the sum scope and we cannot run core routines without a co-routine scope so coroutine scope is a way to keep track of all core routines that runs inside it so uh corrupting scope takes quote in context as a parameter and the quoting context is a set of various elements and the main elements of a quarantine context are the job of the coroutine it's a dispatcher and according name so i'm going to talk about the jobs separately in some of the future videos but for now we're going to focus only on coroutine scopes so uh coroutine scope and the quartering context are closely related and you can say that the corotin scope formalizes the way the choroiding context is inherited so for example when a chorotine is launched inside the curtain scope of another coroutine it inherits its context and the job of the new core routine becomes a child job of the parent core routine so when the parent core routine is cancelled all its children are cancelled as well and the parent core routine always waits for competition of all its children by default now you might have already heard about global scope and the global scope is used to launch a top-level core routines and it lives as long as the whole application lives which means if we use global scope for example in fragment or activity it will not be cancelled even the fragment or activity is destroyed so now you can assume that this can lead to memory leaks so bottom line the main point is that each chord in scope should be tied to a specific application component life cycle and even though the core team scope itself provides a proper way to cancel long running operations automatically there are already some built-in quartin scopes which you should use on a regular basis and those scopes are a life cycle scope and view model scope so as the name suggests the first one lifecycle scope can be used inside activities and fragments and it's tied to their life cycles and the second one viewmodelscope is used in models and it's tied to a life cycle of a view model of course so behind the scenes they handle all the cancellation automatically so for example whenever we use a viewmodelscope it automatically cleans up after itself when unclear uncleared method is called so we don't have to worry about memory leaks anymore okay so enough theory and now we're going to open up android studio and i'm going to show you some practical examples so here i have created just a simple project so i have one activity and two fragments and as you can see this is the first and default destination inside my navigation graph so uh here we have our first fragment and when i press go button it will go to this second fragment and when i press this back button it will go back to our first fragment and i'm going to use that to show you uh some examples a little bit later but before that i'm going to show you how to create your own chord in scope so inside this fragment i'm going to show you how to create one so basically all you need to do is just create a simple variable so i'm going to name this variable scope and here i'm going to use the corrupting scope okay so here as you can see inside this second routine scope we only need to pass the quotient context and as i already mentioned quoting context is a set of elements and the coding context contains the actual job object then a dispatcher and according name so here we don't need to specify the first two we can only for example specify the one so i'm going to specify here a quality name and i'm going to name this scope for example my scope and if i don't specify the actual dispatcher here then it will use the default one and basically with just one variable we have created our scope our coordinate scope and now for example inside this oncreateview method i can just call this scope and i can call the extension function name the launch to launch a core routine okay so inside this core routine i can just type for example uh log to log the actual uh message so i'm going to write here uh co routine and for the actual value i'm going to use uh this dot uh quoting context and basically i'm going to log this quoting context so let's run the app and let's open up our logcat so you can see the actual value okay so here it is uh we have logged our query in context and here as you can see uh first we have our name of our coroutine or our scope so the name of our scope is a my scope as we have already defined inside our scope variable on the top then here we have a job and this is the actual and unique id of our corrupting job and of course this is the actual dispatcher so as you can see we are using this default dispatcher because we haven't specified the actual dispatcher which we want to use and for example we can specify here a different dispatcher so i can write here this patchers dot io for example and here we don't need to add comma we just need to add a plus symbol so like that and that's how we are specifying the actual quarter in context so basically we can specify this dispatcher then plus and the quality name okay so i can set here this variable to private okay so now let's run the app again and you will see that now this dispatcher will change because we have changed this dispatcher inside our scope on the top and now as you can see this dispatcher is using this dispatcher io and now you see how easy it is to actually create your own core routine scope but of course you would need to cancel this according scope by yourself and i'm going to talk about the corrupting cancellations and uh quoting jobs in some of the next videos but for now we're going to only focus on uh scopes so for example uh inside this uh corrupting builder lounge i'm going to add one more uh routine so let's use this lounge builder and here i'm going to log basically the same thing so here i'm going to write core routine and here i'm going to write this dot code in context okay so let's run the app now and let's open up the locket so now you can see we have basically nested this uh new core routine inside this uh first core routine so as you can see here both our core routines are using the same scope and it's because this second coroutine has inherited this uh scope from our first uh corrupting builder and the only difference here is that we have a new id for our quoting job so basically a job is a simple object and every launch uh quoting builder is returning a job object so i'm going to show you that so for example let's create this uh variable named variable and you will see that the type of this variable will be a job object okay and the job is a cancelable thing with a cycle that collominates in its competition and jobs can be arranged into apparent child hierarchies where cancellation of a parent leads to uh immediate cancellation of all its children so basically a job is responsible for coroutine's life cycle cancellation and parent child relations so we can rename this variable now to job so basically uh every coroutine builder lounge will return a job object and we can use this job to actually cancel our core routine but of course i'm not going to talk about jobs in this video so i'm going to skip that for now okay so now you saw how to create your own according scope and now i'm going to show you how to use a global scope so for example we're going to delete this scope here and i'm going to call a global scope.lounge so as i already mentioned this global scope will live as long as our application lives so i'm going to prove you that and here inside our global scope i'm going to write something different so i'm going to write here while is true so basically i'm going to create an infinite loop here and i'm going to add the one delay function of one second okay so just like that and here i'm going to just uh say for example uh running so now uh every second this log will be executed and you will see that even if we destroy our fragment this uh routine will run and now basically i'm going to override a few methods so i'm going to override on pause i'm going to override on stop method then i'm going to override on resume and of course at the end on destroy okay so now inside each and every function here i'm going to add the one log statement so here i'm going to basically uh type here on pause and i'm going to copy that right here and change to on stop then here to on resume then uh down below on destroy okay so let's run our app and let's see what will happen so the first function which was called was on resume then we have this running log running each and every second and even if we navigate to our second fragment as you can see on pause and on stop were called and even then uh this recording continues to run so as you can see running a log each and every second so uh on destroy method that was not called because our first fragment was not destroyed and now let's try and destroy it so we can see if our global scope will still run in the background so i'm going to go to this navigation graph and now i'm going to select this arrow which is pointing from the first fragment to the second fragment and here where it says a pop behavior i'm going to select first fragment and here i'm going to check this option to true okay and now basically when we run our application and when we navigate to this second fragment then this first fragment will be destroyed so now let's check that out and let's run our app again okay so as you can see the first method was called on resume then we have this running then running every second so now let's go to our second fragment and let's see what will happen okay so on pause on stop and on destroy so now basically our first fragment was actually destroyed but still our core routine is running in the background so as you can see this global scope core routine actually lives as long as our application lives and now when we get back to our first fragment as you can see the first fragment will be recreated once again so now you saw in this practical example that global scope actually lives as long as our application leaves okay so now i'm going to show you how to use the lifecycle scope and for lifecycle scope you need to have a one specific dependency so you can see you need to have those two dependencies for core routines and if you want to use a life cycle scope then you should have this implementation or this dependency and for a view model scope you should have this one okay so i'm going to put this source code on my github profile so you can download or copy those dependencies and now we're going to use the life cycle scope and you will see that uh that our core routine will actually uh cancel when we actually destroy our first fragment so now instead of this global scope i'm going to use a live cycle scope okay so this one and now let's run our application again we're not going to change anything and now let's see what will happen if we navigate from our first fragment to the second fragment so let's run the app let's open up this vocab okay so uh as before we have received this on resume first then our core routine is running freely and now let's try navigating to our second fragment and let's see if this corrouting will still be running so as you can see on pause on stop and on destroy were called and our coroutine actually stopped and basically that's the proof that our life cycle scope is actually cleaning up after itself so whenever our fragment is destroyed the core routines will be cancelled automatically and this also applies the same to a view model scope as well and view model scope will cancel itself after uncleared method is called so the best practice is that you should always use a lifecycle scope and a viewmolescope wherever is possible and you also saw how you can create your own custom scopes so basically you just need to use this corrupting scope and of course inside this constructor you can pass different kind of elements and we have passed here the actual dispatcher the quality name and we can also pass the actual job okay so that will be all for this video write down in the comments if you want to see more videos about kotlin core routines please like this video if you find it helpful of course and see the next one
Info
Channel: Stevdza-San
Views: 9,521
Rating: 4.9590793 out of 5
Keywords: kotlin, coroutine, coroutines, suspend, function, basics, explained, video, tutorial, guide, how to, use, what is, what are, android, development, asynchronous, synchronous, execution, programming, thread, multi, multi threading, main, dispatchers, dispatcher, io, default, worker, ui thread, with context, withContext, scope, scopes, builder, builders, viewModel, view model, lifecycle, launch, async, delay, delay(), the basics, introduction, intro, learn, android studio, java, context, coroutine context, job, coroutine name
Id: kXSBkAA03Tc
Channel Id: undefined
Length: 12min 52sec (772 seconds)
Published: Tue Dec 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.