很多人在使用闭包的时候容易引起循环引用,解决循环引用的问题可以使用 [unowned self] 和 weak var weakSelf = self 来解决具体的使用🌰如下
使用weak关键字
private func loadDataAction(){
QMUITips.showLoading(in: self.view)
weak var weakSelf = self
YHLNetworkManger.share.addWorkFlowTableInfoAction {
(flag, data) in
let view = weakSelf?.view ?? UIView.init()
QMUITips.hideAllTips(in: view)
if flag {
weakSelf?.congigerDataAction(data: data)
}else{
QMUITips.showError("表单加载失败", in: view, hideAfterDelay: 1.5)
}
}
}
使用[unowned self]
private func loadDataAction(){
QMUITips.showLoading(in: self.view)
YHLNetworkManger.share.addWorkFlowTableInfoAction {
[unowned self] (flag, data) in
QMUITips.hideAllTips(in: self.view)
if flag {
weakSelf?.congigerDataAction(data: data)
}else{
QMUITips.showError("表单加载失败", in: self.view, hideAfterDelay: 1.5)
}
}
}
直接说结果吧,第一种效果好,因为在网络回调之前页面如果退出,不会崩溃。