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
Try the following:
object.setCompletionHandler { [unowned self] (error) -> () in
if(!error){
weakSelf?.tableView.reloadData()
}
weakSelf?.progressHUD?.hide(false)
}
If you assign a closure to a property of a class instance, and the closure captures that instance by referring to the instance or its members, you will create a strong reference cycle between the closure and the instance. Swift uses capture lists to break these strong reference cycles. source Apple
source sketchyTech First, it is important to make clear that this whole issue only concerns closures where we are assigning "a closure to a property of a class instance". Keep this in mind with each rule. The rules:
In answear to your question there should be no retain cycle.
You stated that progressHUD is retained by the owning view controller (self) and you reference it in your closure...so add it to the capture list and then use the captured variable in the closure as follows:
object.setCompletionHandler { [weak self] (error) -> Void in
if(!error){
self?.tableView.reloadData()
}
self?.progressHUD.hide(false)
}