假设我有一个在UIViewController
子类中的以下方法:
- (void)makeAsyncNetworkCall
{
[self.networkService performAsyncNetworkCallWithCompletion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicatorView stopAnimating];
}
});
}];
}
我知道在这个块内引用self
会导致UIViewController
实例被块保留。只要performAsyncNetworkCallWithCompletion
没有将块存储在NetworkService
的属性(或ivar)中,那么我认为没有循环引用,对吗?
我意识到上面的结构将导致UIViewController
被保留,直到performAsyncNetworkCallWithCompletion
完成,即使它已经被系统提前释放。但是,系统是否可能(甚至可以?)在任何情况下释放我的UIViewController
(iOS 6更改了UIViewController
后备CALayer
内存管理方式之后)?
如果有必须使用“weakSelf/strongSelf”技巧的原因,它应该是这样的:
- (void)makeAsyncNetworkCall
{
__weak typeof(self) weakSelf = self;
[self.networkService performAsyncNetworkCallWithCompletion:^{
typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf.activityIndicatorView stopAnimating];
}
});
}];
}
但我觉得这太丑了,如果不必要的话,我想避免使用它。