When do we REALLY need to use [weak self]? 🤔

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • #iOS #swift #softwaredeveloper #iosdeveloper
    Thank you for watching this video 🙌
    Get the code 👉 bit.ly/3tVujfp
    ➜ Leave a tip: www.buymeacoff...
    ➜ Website: www.swiftwithv...
    ➜ Twitter: / v_pradeilles
    ➜ GitHub: github.com/vin...
    ➜ LinkedIn: / vincentpradeilles

Комментарии • 22

  • @v_pradeilles
    @v_pradeilles  2 года назад +6

    Did you find this explanation useful? Do you think of a situation that doesn't fit in my three examples? Let me know in the comments 🚀

    • @jeremymagnier
      @jeremymagnier 2 года назад

      Hi, good video, what do you think of the use of "unowned", in the 3rd example it can be relevant sometimes to use it, it avoids an unnecessary guard let when you know that if the closure is called self still exists

    • @v_pradeilles
      @v_pradeilles  2 года назад +1

      @@jeremymagnier good question! There’ll actually be a video on this exact topic later this month 🤫

    • @jeremymagnier
      @jeremymagnier 2 года назад

      @@v_pradeilles I activate the bell so haha

  • @ShubhamGupta-qo2pt
    @ShubhamGupta-qo2pt 2 года назад +11

    Good example. It will be good if we can have part 2 representing the retain cycle in memory graph

  • @webdavis
    @webdavis 2 года назад +1

    In regards to the 2nd example, leaving a strong reference to self would be a poor decision for the simple reason that the behavior isn't explicit and is therefore not communicated. If I saw this in a codebase I would refactor to a clearer solution. Leaving a strong reference to self is one of those things that may be a shortcut, but will immediately incur technical debt on your codebase.
    That being said, great video! Loved the examples.

  • @michaelvoline6205
    @michaelvoline6205 5 месяцев назад

    Could you also consider an example with multiple nested closures and explain where we need to specify weak self to avoid retain cycles.

    • @v_pradeilles
      @v_pradeilles  5 месяцев назад

      Great question! I've actually already covered this topic in a separate video: ruclips.net/video/2Ot86h_Xu1M/видео.html

  • @KEVINKIM2586
    @KEVINKIM2586 2 года назад +1

    really useful content. your videos have been extremely useful for my iOS interview questions thx!

  • @Noah-qm4ki
    @Noah-qm4ki 2 года назад +2

    Oh!! It's really useful! I am a student studying iOS in South Korea.
    So I wanted to translate it to Korean and post it on my blog to make more people see it
    (I am not using it to make any profit! & I'll put a link to the original video in my post.) is it OK?

  • @rasheshbosamiya1482
    @rasheshbosamiya1482 2 года назад +1

    good example, thank you for sharing

  • @ronnyfenrich
    @ronnyfenrich 2 года назад

    This still makes my head hurt :). I wish the compiler could just figure it out for all cases and create a warning/error.

  • @captaintech8337
    @captaintech8337 2 года назад

    Thanks for the explanation. It's very useful.

  • @MrWvid
    @MrWvid 2 года назад

    Great topic! Another good topic would be when to use unowned. For example in your 3rd example I think unowned would be perfect because when self is deinited handler will also be removed.

    • @v_pradeilles
      @v_pradeilles  2 года назад +1

      The video on that exact topic is coming later this month 😉

  • @poluxsaurus1454
    @poluxsaurus1454 2 года назад

    I’d say it’s only required in the 3rd example

  • @salmansiddiqui9522
    @salmansiddiqui9522 2 года назад +1

    For second example I am trying to crash app without weak self by poping back view controller or making reference nil but its always capturing self and not crashing is it because apple created it ? for our clousres we need to add [weak self ] which escapes ?

  • @anhhanry7167
    @anhhanry7167 Год назад

    thank you for your clip, I've learned something new today, it's helpful, much appreciated, and also I want to ask a simple question:
    do we need to use [weak self] in case of present AlertController or any animation (any code to use in animation: { } ) ?

  • @developerios6096
    @developerios6096 Год назад

    Very informative video vincent, so with closure at line 27 there would not be a memory leak is it correct? There is no reference to the closure, and so, there is no retain to self.
    But diving a bit deeper:
    1)There would be a kind of retain to self anyway, as that closure executes after 2 seconds, even if the ViewModel gets deallocated. Correct?
    2) The ViewModel can't get deallocated, until the DispatchQueue.after is executed. Correct?

  • @Herton17
    @Herton17 5 месяцев назад

    AÏE LIKE YOURE FRENCH ACCENTE. GOUD VIDEO ALRIGHT