Dealing with Retain Cycles (Swift/iOS)

Поділитися
Вставка
  • Опубліковано 16 січ 2025

КОМЕНТАРІ • 19

  • @akaButters
    @akaButters 2 роки тому

    Great example with a clear fix explanation.

  • @fleonardo5801
    @fleonardo5801 4 роки тому +2

    Clean and strightforward.

  • @Shermyyy
    @Shermyyy 4 роки тому +2

    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 9 місяців тому

    Thank you.
    Clean and Straightforward.

  • @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.

  • @waheedafolabi6929
    @waheedafolabi6929 4 роки тому

    Thanks Jonathan...

  • @akaButters
    @akaButters 2 роки тому

    Why couldn’t you just make the WeatherView properties that point back to the WeatherViewController “weak var”?

  • @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.