Dealing with Retain Cycles (Swift/iOS)

  Рет қаралды 2,623

Swift Arcade

Swift Arcade

Күн бұрын

Пікірлер: 19
@akaButters
@akaButters 2 жыл бұрын
Great example with a clear fix explanation.
@Shermyyy
@Shermyyy 4 жыл бұрын
Awesome stuff! I really liked your closure video and totally missed that retain cycle too. Thanks for the transparency and follow-up!
@swiftarcade7632
@swiftarcade7632 4 жыл бұрын
Ya no problem. That was a bit embarrassing. But it happens! Thanks for the comment.
@TheBooban
@TheBooban 3 жыл бұрын
@@swiftarcade7632 Every example I see is with a closure. Why is it always a closure? Is there an example without a closure?
@swiftarcade7632
@swiftarcade7632 3 жыл бұрын
@@TheBooban Great question. Closures are a really easy way to accidentally introduce retain cycles in code. That's why you see so many. I don't have an example of one not using a closure handy. But with some Googling I am sure you could find one. Cheers.
@TheBooban
@TheBooban 3 жыл бұрын
@@swiftarcade7632 i googled. Can’t find one. In a class, what does var a = self do? I think thats a retain cycle too?
@swiftarcade7632
@swiftarcade7632 3 жыл бұрын
@@TheBooban I found one here: medium.com/mackmobile/avoiding-retain-cycles-in-swift-7b08d50fe3ef
@minamamdouh8062
@minamamdouh8062 10 ай бұрын
Thank you. Clean and Straightforward.
@fleonardo5801
@fleonardo5801 4 жыл бұрын
Clean and strightforward.
@swiftarcade7632
@swiftarcade7632 4 жыл бұрын
Thank you sir.
@swoleavocado
@swoleavocado 4 жыл бұрын
if you have one or two closures this might save some typing but I think using a protocol delegate starts to make more sense. Plus it makes sure you don't forget a func. It's always good to be exposed to different approaches though. Thank you.
@swiftarcade7632
@swiftarcade7632 4 жыл бұрын
I agree Avocado. I generally prefer protocol-delegate. Apple seems to be pushing closure more with Swift however, so I wanted to try and see what it looked like. Agree 100% its good to try different things and see what you like. All the best.
@akaButters
@akaButters 2 жыл бұрын
Why couldn’t you just make the WeatherView properties that point back to the WeatherViewController “weak var”?
@waheedafolabi6929
@waheedafolabi6929 4 жыл бұрын
Thanks Jonathan...
@swiftarcade7632
@swiftarcade7632 4 жыл бұрын
Most welcome.
@p16r
@p16r 4 жыл бұрын
At 5:18, since your weatherService is a struct instance, instead of capturing [weak self] you can simply capture [weatherService]. This way you capture only exactly what you need and nothing more. And being a value type you don’t have to worry about retain cycles. And in the closure you aren’t mutating weatherService in any way so you won’t create unnecessary copies of the value at this point or worry about keeping mutations made inside the closure in sync with the value outside. Refer www.swiftbysundell.com/articles/swifts-closure-capturing-mechanics/#capturing-values
@swiftarcade7632
@swiftarcade7632 4 жыл бұрын
Hi Prathamesh - good points. Here the retain cycle isn't between the ViewController and the WeatherService. It's between the ViewController and the extracted View - which are both classes. Hence the need for [weak self] as both use reference semantics. Thanks for sharing. Cheers - Jonathan
@p16r
@p16r 4 жыл бұрын
@@swiftarcade7632 Thanks Jonathan. My point is you don’t need to capture self at all if you only have to use one of its properties inside the closure. You can capture the property instead.
@swiftarcade7632
@swiftarcade7632 4 жыл бұрын
@@p16r Ah my apologies - sorry I misunderstood. Yes you are correct - always best to only capture what you need. Thx for pointing out.
What are Nibs? (Swift/iOS)
22:23
Swift Arcade
Рет қаралды 5 М.
Grouping network calls like a boss - DispatchGroup
14:35
Swift Arcade
Рет қаралды 4,1 М.
Who is More Stupid? #tiktok #sigmagirl #funny
0:27
CRAZY GREAPA
Рет қаралды 10 МЛН
The secret to the UIScrollView
11:44
Swift Arcade
Рет қаралды 13 М.
Swift - Stack vs Heap
9:58
Swift Arcade
Рет қаралды 7 М.
Getting started with the Spotify iOS SDK
27:14
Swift Arcade
Рет қаралды 14 М.
Swift iOS How to update your view when keyboard appears
13:11
Swift Arcade
Рет қаралды 11 М.
MacBook Air M4 Launch Date LEAKED - EVERYTHING You Need to Know!
10:21
Matt Talks Tech
Рет қаралды 2,1 М.
o3-Mini Fully Tested - Coding, Math, and Logic GENIUS
9:58
Matthew Berman
Рет қаралды 57 М.
What are Container View Controllers?
14:30
Swift Arcade
Рет қаралды 3 М.
Day in the Life of an iOS Engineer at PBS
9:32
Shon
Рет қаралды 12 М.