Scoping with HILT (Dagger2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna talk about scoping using Hills now scoping and hilt is pretty similar to how dagger did it the biggest difference for I would say is that it's actually the same as how dagger does it the difference is that hilt generates a bunch of different scopes that are already pre-built and packaged so as you already know when we built our we set up hilt so we got the application class we annotated it with at hilt Android app or whatever that annotation was and it what it does is it generates all the components for you so it generates like an app component it generates an activity component of fragment component well it's generating all those components and it's also generating scopes for all of those components so like you know the app component for example is automatically given the singleton scope the singleton scope will live as long as the application is alive so if you annotate a dependency with singleton it will exist as long as the application is alive the next kind of teardown is then the activity retained scope and by the way if you if you're not sure about what I'm saying we're going to take a look at the docs in a second here it's going to make more sense but what I'm trying to explain is that it's a tier system so the next tier down is the activity retained scope that's one for that would be essentially the same scope as a view model a view model will stay alive longer than an activity but it will die before the application dies if there's a little memory conditions the next scope down would be the activity scope so the activity scope would only live as long as an activity so activity dies all the dependencies that are scoped with the activity scope also die next is fragment scope and so on so I'm sure that was confusing to you that I said a lot of weird stuff there and if you don't know anything about scoping that was definitely confusing to you and you have no idea what I just said so don't worry we're gonna look at the documentation and I'm gonna explain this and then we're gonna go through an example and you will definitely understand what I'm talking about okay so here I am in the hilt section of the Android documentation this component scopes kind of section now the biggest takeaway here I guess is what I'm what I want you to understand is that this is like a tier system so we have an Android class and a generated component like I mentioned so in the application class it generates an application component class and that owns the singleton scope in the view models we have activity retained component and that owns are the are the the scope that is associated with that is the activity retained scope then down in the next kind of tier you have activity then you have the activity component the activity and then activity scope so this is a it's a tier system like the one that will live the longest is of course the application therefore the singleton scope that will live as long as the application is alive then you have the view model which lives longer than an activity but doesn't live as long as an application so it's kind of like a tier system next is activity activity if an activity is destroyed guess what the view model is not destroyed so the activity dies before the view model and then it kind of just goes down the line like that so like still if you don't understand kind of how this is all coming together we're gonna look at an example the biggest thing here is that I want you to know this is a tier system and these are the different scopes that are associated with the various tiers so if you if you annotate a dependency with singleton that dependency is going to live as long as the application is alive if you annotate a dependency with activity retained scope that's gonna live as long as the view model is alive if you annotate it with activity scoped it's gonna live as long as the activities a lot I think you get the idea so now let's go and take a look at an example okay so I'm building on from the previous video where we did some field injection we actually also did constructor injection but I I removed that other class that we were working with because it's not it's not really important I just wanted to outline what what constructor injection is now that you know what it is we're just gonna kind of move forward so I have I'm doing field injection like we were doing still printing out that log statement so now I want to I want to show you an illustration of the different scoping that you can do so what I'm gonna do is I'm gonna scope this with at singleton and I'm gonna run it and you'll see that there is absolutely no issue with me doing that what all it's going to do is make this dependency live as long as the application is alive so there should be no issue so if I pull up the log still we get look I did a thing cool no problem now what I'm gonna do is I'm going to jump down a couple tiers and I'm gonna use a fragment scope so what I'm gonna do is I'm gonna write class you know my fragment extends fragment so I'm a new fragment class I'm not gonna build a layout for it or anything I'm just gonna create the fragment now I'm gonna out tape this with at Android entry point so I'm marking this as a potentially a potential user of dependency so I can inject things into it and now I'm gonna do inject late init var and do some class and inject that class so what I've done here has just created a new entry point a new thing that I've added to the dagger graph which is this fragment and I am in doing field injection and injecting this dependency that is marked as a singleton now remember dagger will check things at compile time so I don't even need to like I don't even need to do like support fragment manager begin transaction Dabra plays I don't need to bring this fragment into view because dagger is going to check this at compile time that's one of the good things about dagger it doesn't check things at runtime it checks things at compile time so if you're gonna get an error it's gonna be when you try and build the project which is good because that way you're gonna get an error as the developer as opposed to a user getting the error which would which would happen if it if it did the checks at runtime so I'm injecting this doing a thing and everything I think I already ran it but I'll run it again and you'll see that everything is fine so there we go I'll actually pull the app on the screen here we don't need to look at the log and just notice that it does actually run so now what I'm gonna do is I'm gonna change the scope I'm gonna change this or sorry I'm gonna change yeah I'm gonna change the scope here too let's go down to activity scope so if we take a look at the documentation again move this over a little bit so we have application which is the singleton I skipped over activity retained scope and I moved to activity scope so I've created this dependency and it's scoped to activity scope now let's go back to Android studio let's run this again and see if our app will build and everything is good yes the app builds everything is fine so now what I'm gonna do is I'm gonna go to the documentation and I'm gonna go down another scope I'm gonna go down to fragment scoped so if I change this to fragment scoped and I run this what is gonna happen so notice I get a compile time error it brings up this this my application hilt component and it's giving me an error here saying activity scope may not reference bindings with different scopes so the problem here is if I'm trying to inject something into an act that is fragment scoped that's not that's not good so if I if I actually comment this out I'm gonna comment out some class being injected into the activity and I'm gonna run this again and you're gonna see that everything is actually fine which is probably not what you expected so I'm running it the app is launching and there we go everything is fine so the reason it's fine is because the activity is part of the activity scope if we take a look at the chart here the activity is part of the activity scope but my dependency is actually fragment scope and because this is a tier system it's like a tier system that goes downwards so like the anything that is activity retained scope cannot be injected into something that singleton scope anything that's activity scoped cannot be injected into activity retained scope anything that's fragment scoped cannot be injected into activity scope and that's what's happening here I have something that's fragment scoped and I'm trying to inject it into something that is activity scoped and that that's not okay that's not gonna work but if I inject it into something that's fragment scoped which is the fragment everything is fine so if I was to change this back to like activity scoped and I run it again whoops and I saw a comment comment back in all of this stuff and then I run it again everything will be fine because I'm injecting it into something that's activity scoped or lower because fragment comes kind of after the activity scope if you look at this chart here so if I if I have a dependency that's activity scoped I can inject it into that I can inject it into that I can inject into that or that it kind of goes downwards now that we've talked about scoping and kind of this tier system and how it works on hilt how it generates components and ties certain Android things like activities fragments services the application class to that component and to that scope now we're gonna go and we're gonna take another look at constructor injection since that's the type of injection that you're going to be using pretty much all the time we're gonna take another look at constructor injection and go through some of the issues with constructor injection and how to solve them because constructor injection doesn't always work it's not always simple to do there's certain cases that you'll run into and they're actually they happen frequently and you need to know how to work around these issues and hilt has built-in systems for you to work around those issues so I'll see you in the next video oh actually I forgot just before you go don't forget to go to coding with mission komm if you're watching this course on youtube you can also watch it on my website and it will keep track of your progress it's completely free you just have to register an account just go there go to the course it's easy to find it's the courses page you know it's a very simple website find the course and you can you can watch it it'll track how far you get through a video what video you're on when you complete the course all that kind of stuff so that's going to be it I'll see you the next one you
Info
Channel: CodingWithMitch
Views: 17,577
Rating: undefined out of 5
Keywords: dagger2, dagger-android, dependency injection android, android dependency injection, dagger android, hilt android, android hilt tutorial, android dagger hilt, android dependency injection with dagger2, dagger 2 kotlin, dagger 2 kotlin tutorial, dagger 2 kotlin mvvm, dagger 2 kotlin android, dagger hilt
Id: ZVv8mh1Kols
Channel Id: undefined
Length: 9min 39sec (579 seconds)
Published: Tue Jul 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.