I am trying to resolve a closure based strong reference cycle in Swift.
In the code below, object is retained by the owning view controller. ProgressHUD
is a UIView
that's also retained by the owning view controller. ProgressHUD
is leaked every time the completion handler is called. When using the new closure capture feature, declaring self as weak or unowned does not resolve the memory leak.
object.setCompletionHandler { [weak self] (error) -> Void in
if(!error){
self?.tableView.reloadData()
}
self?.progressHUD?.hide(false)
}
However, if I declare a weak var for self outside of the closure, it fixes the memory leak, like this:
weak var weakSelf = self
object.setCompletionHandler { (error) -> Void in
if(!error){
weakSelf?.tableView.reloadData()
}
weakSelf?.progressHUD?.hide(false)
}
Any ideas as to why this is not working with Swift capturing?