Home > front end >  Do you need unowned or weak inside a UIAlertController handler which captures self?
Do you need unowned or weak inside a UIAlertController handler which captures self?

Time:11-10

Does a UIAlertAction's handler need a [weak self] or [unowned self] to avoid a retain cycle, or is it fine to keep it as a strong reference?

e.g.

extension UIViewController {
    func showAlert() {
        let alert = UIAlertController(
            title: "Foo",
            message: "Bar",
            preferredStyle: .alert
        )
        alert.addAction(
           .init(
               title: "OK",
               style: .default
           ) {
               self.doSomething()
           }
        )
        present(alert, animated: true)
     }
}

The handler closure retains self. alert is never explicitly retained by self, but does self retain it implicitly because of the call to present? How would you determine whether that is the case? Maybe this uncertainty is sufficient justification for not using a strong reference…

I know when I should use unowned versus when to use weak. My question is about including either of those two versus including nothing, i.e. keeping it as a strong reference. There are lots of blog posts and questions about this but most of them focus on weak versus unowned and don't really expand on when you don't need to use either of them.

I know practically you could just always put [weak self] everywhere and it's not really a performance concern. But I'm trying to understand when it's really necessary and when it is just noise. Using an alert as an example here, but I'm interested in developing a sense for this in general as well so I can justify myself when I choose to include or exclude a [weak self] or [unowned self].

Is a retain cycle the only concern with strongly referencing self inside a closure?

CodePudding user response:

The alert, it's true, has a strong reference to self. But self has no strong reference to the alert! It just makes the alert, presents it, and drops it. Therefore there is no retain cycle.

The alert, as long as it is showing, continues to retain self and prevents it from going out of existence. But self should not go out of existence while the alert is showing, so that's okay too.

  • Related