Weak and Unowned Self Closure Memory Leak Fixes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey what is going on everybody welcome back to the channel hope you're having a fantastic start of the 2019 year in today's video I just wanted to make a very very quick lesson on why sometimes we have to use a weak self or an unknown self inside of our closure blocks and so the way I'm going to demonstrate this is to use this a very very simple application and right over here so let me just run this right now and what you'll see is at the very beginning we're going to show this black screen a view controller right over here view controller and we have the little button on the top right that shows our Reddick controller down here so nothing too fancy going on we're just setting up a couple of buttons and then showing there Reddick controller here and so let me just click on show red and here we go we have our awesome a red table view controller now the whole point of this very very simple demo here is to show you that whatever I hit this back button right here we're going to correctly D initialize their red controller and let me just click on that and you can see on the bottom right here the OS is and now reclaiming the memory for our red controller which means that we currently do not have any type of retain cycle or any memory leak okay so that's the point of this a rather silly demo here and what I would like to do now is to move on to introducing a routine cycle by using a closure on the notifications Center so what exactly do I mean well let me show you what I'm talking about by going into the viewdidload and right below the table view of a red and background color let's see I'm going to type something out over here so notification in the center retains cycle with closure I'm going to do that right here and you can see down below I have this function that's called show alert creates a alert controller has a couple of things inside of it and just presents it so again a really silly function right here I'm going to call this within my closure that I'll introduce right now so the actual code is rather simple I'm going to attach an observer on to the notification center via this call here so you'll be asked to fill in this guy with the notification name so you know some notification name and the the object you can say no the queue you can just use main like so now whatever you type in this code is going to tell you to fix this notification by clicking on that and produce the actual type of notification name and then the raw value of this string okay so make sure you fix that and for the closure right here just hit the enter you'll get the notification to show up just like so now this little bit of code is used pretty commonly within iOS applications and the problem with this code is unless say whenever we actually get a notified of this particular notification instead of our app let's say that inside of the closure we respond by calling this show alert function and right below okay now I'm going to run this application again and for those of you guys that are unaware there is already a retain a cycle that we're introducing by using these self and let's confirm that the routine cycle is actually there by clicking on show read and Haley hitting the back bend just like so to pop our read controller off and you can see that in the bottom console area we're we're not really seeing this D initializer print statement of os reclaiming memory so that's how we can confirm that there's a memory leak we should be seeing that down below and now you might be asking well what is causing our memory leak well whenever you use these self like this inside of this closure here the Notification Center is actually capturing self as a strong reference inside of Notification Center and what that means is when you're trying to deallocate the memory for this red controller right here it means that Notification Center has a reference to it so it's not able to correctly deallocate that space all right so notification has a reference to red controller through this self variable here and the way to actually fix this is to go ahead and say week and self right in front of the variables for your closure so for example we have notification here you know I'll try to run the code again whenever you introduce self as a week like this you have to make sure to use a question mark for the optional chaining like that and the reason why that is is because a weak self is now an optional variable that will later on be and milled out so don't be too concerned about what that means but basically you need the question mark there you hit the show read now and then you hit them back you'll see that now the routine cycle is no longer there and the OS is able to correctly to reclaim the memory that was allocated to the read controller itself okay so that's pretty awesome another way to get around this problem here is to use something called an unknown so unknown itself like that and you can remove this question mark like so now the difference between the weak and the unknown is pretty difficult to explain but if you're able to guarantee that this self guy will never be nil then you can use the unknown self alright so show red and hit the back again you'll see that the unknown is also able to fix the memory problem that we saw before with the retain cycle you still see this being printed down so everything looks good up to this point of the lesson alright so it's kind of what I wanted to show you in today's video and I guess before I end today's lesson maybe I'll talk about something else as well and let me just comment out this code and let me run it again I want to make sure I see this inside of the console and I'll show you how to how to show your retain cycle by using a local variable instead of social read and back you see that so that looks good so here's what happens sometimes when you do something like this you can say var and you know something like refresh table table view closure you sometimes you might have some kind of closure variable like this that takes in some data maybe some air and let's see and this is your closure here and if you have this closure you can declare a roof as refresh table view closure and set it equal to some kind of closure make sure to declare the variables data and air like so in like that alright so everything looks good here and if you try to call self so our alert is very similar to what we did over here and you'll see that this also creates that retain cycle so show read hit the back you'll see that our print statement is not being printed out that means we can't reclaim the memory for read controller and so in this specific use case you can also say weak self and that will help you break the retain cycle so again very similar to the Notification Center code down below we're going to hit show red and hit back again you'll see this being printed out so that's pretty good and one last thing that you might be concerned about is sometimes you'll have this service class it helps you make API calls and I guess I'll try to define this as a singleton so shared equals service so let me try to type this out here this guy will be a static class variable and you'll have some kind of function so function of such data and you'll have a completion block here and the completion block will be something like this and then you'll you know close it off and perhaps this will be some kind of error variable and an inside of your function here you probably want to call completion like that and just build your project this should be good I'm going to pass in a nil because the completion block expects you to complete with some kind of variable so that's what your service class will you know look like maybe but eventually inside of your other classes for example your red controller you might want to call that service singleton so shared dot fetch data you have this closure hit enter you have the potential error you know check the error I guess something like that return and so down here you can call self show alert as well and so what I'm going to do you know this is just a warning I'm going to try to see if we have a retain cycle using this code right here so you'll be a little bit surprised as to what's gonna happen we're going to hit show red we're going to see that we are showing this alert message because this bit of code is being called immediately right here so completion block so instead of doing this why don't I make this a little bit more realistic so URL session sheared and did a task and we will do something like this right here so this is usually what happens right so garlotte URL equals some kind of URL string and you know HFS maybe google.com something like that here is our URL I guess somebody that else returned and that looks good that that that and over here you can just call completion with something like nil all right so this isn't exactly supposed to make a whole lot of sense but I just want to run this again and maybe it will print the error down here I feel like this should run I'm not going to run the URL session so what I'm going to do now is I'm going to show the red and try to confirm what's happening over here and you might assume that using myself like this inside of this closure will produce a routine cycle right so you go back and that's actually what it does you see self over here will also give you your routine cycle and what you want to do is to make sure that you break it and say weak self like that and over here you're going to want to use the question mark to access your weak self variable and now what I can do is I can run this code again and hit the show red hit the back just like that and now you will freely kind of break your retain cycle and get the very important memory that you used to allocate for red controller alrighty everybody that's gonna wrap it up for today's lesson if you enjoyed today's video make sure to hit the like button down below if you want to support what I do make sure to check out the couple of courses down in the description below that's gonna be it for me today and keep on coding guys I will see you in the next video bye bye
Info
Channel: Lets Build That App
Views: 41,527
Rating: 4.93188 out of 5
Keywords: ios, swift, development, tutorial, learn, xcode, programming, code
Id: q0-DIJszYRo
Channel Id: undefined
Length: 12min 20sec (740 seconds)
Published: Tue Jan 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.