Start Using Lazy | Swift 5, Xcode 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody it's your boy kilo loco and today we're gonna be going over when you should be using the lazy keyword and swift now I'm gonna try to break it down in the most basic form possible so that you can easily understand why you would want to use lazy and then hopefully you'll be able to go out there and start using lazy in your own projects understand why it would be a huge benefit when you start using it now before I get into that I just got to say make sure you head over to kill loco comm check it out see if you're interested in the all-access membership so that you can talk to your boy right on slack if you have any problems at all just hit me up so yeah alright so let's jump into the project and as always the project is going to be in a link in the description down below you can download it for free just keep in mind this is in Swift 5 so you will need to be able to have Xcode that can run so with 5 if you want to compile this but you will have access to the project anyway as you can see we're just gonna be staying inside this basic view controller and I actually have this I have the similar up showing exactly what the feature what this app is going to do now let me go ahead and run this one time so that we can see what's going on alright so I want to show you exactly how fast this app is gonna open look it so you see the the beginning screen real quick and then you're gonna see the welcome screen and then we're gonna have a button that's not really gonna do anything you just click it and nothing really happens and there's also this other button that says get expensive number now right now the functionality that's related to it is actually commented out as you can see right here in write right here in this comment I have something called let expensive number equals awesome serviced I get expensive number and then we're setting the text to that expensive number now let's go ahead and take a look at our awesome service just to see what it's doing real quick the awesome service is simply just creating a number I'm going from 0 I'm looping from 0 to 1 million five hundred thousand and then I'm printing out each of these numbers in the logs right and then at the end of that I'm just going to return that number right here so all this is doing is just doing a huge loop that's gonna take a while for our processor to finish and then it's gonna put it into this constant right here now I just want to show you one last time how fast the app opens when we run it so finish building you see the screen real quick and then it shows welcome right well let's go ahead and uncomment this and I want to show you what happens when you don't use lazy so essentially what we're doing is as soon as we are creating this view controller we're also getting this expensive number and setting it to this property right here so that means that it has to go through that 1,500,000 loop before the view controller can actually get going and do the the viewdidload in anything else so let's go ahead and run that so that you can see how long it's actually going to take for this app to open now look at how long it's gonna stay on this screen and you might have seen this in actually other apps where it takes a while for the app to actually start up you're just stuck on the launching screen which is really bad you don't want that right you don't want to be waiting forever for your app to open before you actually get to see the functionality so what's actually happening is like I said this expensive number is being calculated before anything else is actually happening before the viewdidload actually runs which we can see right here in this print statement that's saying loaded it's down here let's take a look at the logs real quick you can see that it looped through it's going through all 100 or 1,500,000 loops printing out that number and then finally when it's done it will eventually hit the viewdidload so that's really bad we don't want that now the benefit to doing something like this is that when we go ahead and uncomment this our our did tap get expensive number button I want to show you how fast we're able to calculate that so here comes the app we're waiting on the launch screen for a very long time and I'm actually gonna leave this in here just because you know you should see how bad this can actually be if you're if you're not using lazy so this is a problem that lazy is going to solve it's gonna make it so that we launch faster now when we get the expensive number the calculation has already been done so we're immediately getting this label to update right so let's go ahead and turn this into a lazy property so lazy properties always have to be variables because they're actually not they're actually not assigned a value until they're ready to be used so lazy you can think of it lazy is like saying procrastinate wait until the very last second to do something and then calculate the value so what we're gonna do is we're gonna use the keyword lazy and we're gonna say that it's a variable because like I said all lazy properties have to be variables because they won't have a value initially so now let's go ahead and run this I want to and I want to show you how much faster the app is actually going to launch compared to how it was before so we saw the the launch screen but it immediately disappeared right and also take a look at our logs viewdidload loaded there's no looping through all these different numbers right it immediately ran and we got the loaded now the awesome thing about lazy is that like I said it's saying procrastinate until the last second to when you are actually expected to be used essentially right so that means if we're over here clicking some other feature we're doing all these other things in the app the expensive number is actually never being generated and we can also verify that in our law or in our debug area if we were to just say oh let me let me go ahead and run this one more time we're gonna need to use a breakpoint right here and I just want to show you that expensive number is actually set to nil at this point so if we go on this left side and if you take a look at expensive number storage the value is actually nil at this point so what I want to show you is we're gonna go ahead and press play we're gonna continue from this breakpoint all right and now what I want to do is when I hit get expensive number what it has to do is it has to do that calculation where it loops through all 1,500,000 but notice it doesn't do that until we actually are are going to call this functionality right here so now when I hit the get expensive number notice that my UI is actually freezing up the button didn't returned back from that highlighted state and we still haven't updated the UI because down here in the logs it was still going through that looping process now once it finishes going through that looping process we're eventually going to update the UI and we're gonna get back that expensive number so this is an exaggerated case of what how lazy can benefit your app is because you can get into the app immediately right and or you can load up some type of screen some view controller or whatever and you can use all these other features and if none of those other features are expensive to use which means that it takes processing power and RAM away from the rest of your app to actually calculate what needs to be calculated then you're gonna be fine there's not gonna be any lag in your app or anything like that but as soon as you need to do something that's expensive where it's using up a lot of processing power that's when it's actually going to be created and at that point if it's something expensive then it's that's how long it's just gonna take I mean there's ways to get around that but that's a discussion for another video so that's the main benefit of using lazy that's exactly what it was built for now there's another there's another case where you would actually want to use lazy and it's mainly for I guess a developer preference and that's if you want to create your views using lazy views so let me go ahead and show you how that works alright so as you can see we have this different structure that are this different um you know structuring or programming layout I guess you could call it essentially this different format that you might not be used to seeing so once again we're gonna mark this this some image view we're just going to call it some image view and it's a type UI image view now once again we're marking with lazy it has to be a variable we also have to specify we have to explicitly specify what type it is because it actually won't have a value until you know once again it's lazy so until it's actually about to be used so what we have to do is we have to make sure we specify what type it is in this case it's a uiimageview and then we have it equaling essentially a closure this closure right here now it's equally in a closure and inside this closure or a block of code we're gonna do whatever we want but essentially what we need to do is we need to make sure that we're returning something of type UI image view or whatever type is right here right so we're gonna go ahead and create an image view and what's really awesome about this approach is that you're actually able to manipulate the image view right here inside of this closure and we can you know we could add the image to it we can change the background we could do we can mess with all the different properties that are related to this image view and it keeps all of our code kind of concise and in in one spot so that we want it so if we want to change something about our image view all of its in in the same block of code all in the same spot so what I want to do for this image view is I want to set the image so I have this I'm setting it to this UI image named KL meme og which I have in my assets folder which you can see right here and it's just gonna have this little image so now everything that's kind of associated with the image view setup is all concise and compact into one spot now another thing that I will also want to do is maybe I want to say how big this image view is give it a frame so let's go ahead and add in a frame so now that I actually have given our image view of frame since we're not gonna just be using auto layout in this particular example what I want to do is I just want to add this image view I want to make sure that it's centered in our view and then I also want to add it to the sub view now you can essentially do that here but I usually recommend staying away from any franciene self outside of this outside of our inside of the closure you don't want to reference self inside of the closure I mean you can but there's a potential for use for getting a retain cycle and if you do really want to use self inside of the closure what you're gonna have to do is you're gonna want to say unknown self in a capture list like so but it's just best if you stay away from doing that because realistically you don't want to have all your layout you kind of want your layout all on the same area you don't want your layout to be set up in here so let's go ahead and add that to the view the view will appear alright so as you can see we're just gonna go ahead and set the center of our some image view to the center of our view and then we're going to make sure that we add some image view to our view using the add sub views method so let's go ahead and run that and see right now that not only is our view or not only is it our view gonna be right there in the center but it's also going to be laid out properly and we have all the functionality however we wanted to set it up you know the image the the width and the height all that stuff kind of set up all in one spot which is really nice and then once again I still haven't got the I haven't retrieved the expensive number yet so I can go through do all the other features and all this other stuff that I want it and then I if I never end up getting that expensive number then I never have to worry about you know it going through that loop and freezing up my UI you know so I hope that makes sense I try to explain it in the best way that I can but if not you know just let me know hey could you explain you know this other thing to me so yeah that's pretty much lazy it's just waiting to the last second to get up and running it's awesome for you know when you're doing something expensive you want to make sure that your views brought you know you want to make sure that you're not doing expensive work if you're not going to be using that value and it's also awesome for making sure that all your code is in one spot if you're going to be using views and things like that last thing that I just didn't mention was that you need to make sure that you call it at the end of this closure so now are you writing not what's supposed to happen but you're also calling it so you need to put the parentheses at the end of at the end of the closure but that's pretty much it hope you guys like the video hope that you thought it was informative if you did make sure that you share it with as many people as you possibly can and that's gonna be all for today guys thank you for your time and make sure you go out there and keep coding passionately you
Info
Channel: Kilo Loco
Views: 12,412
Rating: undefined out of 5
Keywords: swift helper service, swift best practices, whats new in swift 5, swift lazy keyword, what is lazy in swift, how to use lazy swift, swift just in time, swift slow loading, swift faster app, swift optimization, swift unowned self
Id: ByEY4mfSAPM
Channel Id: undefined
Length: 13min 59sec (839 seconds)
Published: Mon Feb 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.