Dealing with Retain Cycles (Swift/iOS)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hi everyone welcome to the swift arcade i'm your host jonathan rasmussen it's true yesterday i was posting some code about abused enclosures and i wasn't particularly careful about how i was using them i introduced a retain cycle into my code without even realizing it and in this episode i want to show you the mistake i made what a retain cycle is and how you can fix them in your projects okay before i show you the problem i introduced in the code yesterday let's go over quickly what a retain cycle is basically retain cycle is any time you have two classes in swift and they have strong references to each other for example take this book in a page here i've got a class called book it has a variable of pages pages is also a class it has a reference back to book this creates what's called a strong retain cycle meaning even if page goes away book still has a reference to it but even if book goes away pages still could have reference to it and these cycles reference each other meaning when one gets cleaned up or gets removed from memory it can still contain a link or a reference to the other one hanging around because variables are objects and they have strong reference types by default and swift this is something we need to be careful of when working with classes anything with the reference types and a particular ui kit let me show you what this looks like in a sample app now in the example i was working on yesterday i showed how basically we can extract a view and then use closures to communicate back from the view to the view controller in our weatherly app here so when someone comes up here and searches for the weather in a particular city i can have that extracted view communicate back by setting a closure or invoking some code when the search field is activated so this is really elegant technique we define a closure here which passes back the text field and then in our view controller we can paste basically pass in or set the function we want to call when that closure is called so in this case in my weather view controller i've got an instance pointing to whether view remember these are both classes now so the fact that i'm in a class and i'm pointing to another class and passing in a closure that introduces the retain cycle and that's what i didn't see yesterday so here's a very simple example of a class which defines a closure and then references references itself in the closure that's what sets up the retained cycle that's what's dangerous that's what we want to avoid there's two ways we can break this one is we can use a capture list to basically define the reference to ourself as weak in the closure this is great because now instead of self having a strong reference here by making this week that breaks the cycle and now we've only got the strong reference going one way and a weak reference going back to self the other that's one way of breaking the retain cycle this is probably the safest this is the thing you'll normally want to do when breaking retain cycles and it's really our go-to method for doing it there is another keyword which can be a bit confusing and swift but it does have its time and place and that is unknown self this is basically just like weak the only difference here is this makes this a unwrapped version of self so now you can go in here use self directly you don't need to unwrap the optional like we did up here and basically you would only ever use this if you were absolutely certain that self was going to be there when you needed it in other words if it's not this will cause a runtime error and blow up if self isn't there so you only want to do this in those cases where you're absolutely sure and you want to save yourself a question mark here but the safer thing to typically do is go ahead and use the weak self and break the retain cycle that way so getting back to my app how am i going to solve this problem of breaking the retain cycle between the view controller and the view well basically what i can do is go ahead and use that weak self guidance here in my view controller instead of directly passing in the function fetch weather when i go to set that closure on the view what i can do instead is set up a function that sets that for me so basically instead of going view controller search fetch weather passing in like this i can create a function called setup search handler and what that will do instead is that will create an instance of the closure that i actually want and here's where the magic happens when i go to define it i can define it as a weak self so now i don't have that strong closure back i can go ahead and use the text field passed in here's where my optional self is set so i can go ahead and do the work that i was doing down here in my previous fetch weather method and that's how i can safely set this closure in my view controller by making the reference back to myself weak now if i really wanted to get you know fancy one thing i could do instead is i could inline this up here so if i preferred you know this syntax over setting it here i could come up here and in line this just like this but because i prefer keeping my functions at the same level of abstraction i thought this was getting a little bit too deep here and i prefer just handling it like uh like this with the search handler there so that's how we can break that and if i just wanted to do it for the other one i also have a location uh weather handler that i could come in here and set up like this and that would also set up my location one so basically instead of passing it in like this i could also go set up weather result handler get rid of that call there and then i no longer need these functions down here because i'm now doing them in the closures that i've set up there well there you have it friends that's an example of what a retain cycle is how you can see them in our projects and really whenever you're working with closures that is something you need to be aware of i totally missed it yesterday when i created that demo and i inadvertently showed you how to introduce a retain cycle into your app but basically just by being aware that every time you're defining a closure it's really good to check to see whether you're referencing back to yourself you can solve these problems by creating a capture list passing in weak self and breaking that retain cycle and it's just it's just a fact of life and something we do need to be aware of when working with ui kit and swift when we're working with those stronger reference types okay i hope you enjoyed that thanks so much and we'll see you next time take care bye [Music]
Info
Channel: Swift Arcade
Views: 682
Rating: undefined out of 5
Keywords: swift, ios, iosdeveloper, softwaredeveloper
Id: iJ5tjwEnGEI
Channel Id: undefined
Length: 7min 41sec (461 seconds)
Published: Sun Jan 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.