假设我在UIViewController子类中有以下方法:
- (void)makeAsyncNetworkCall
{
[self.networkService performAsyncNetworkCallWithCompletion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicatorView stopAnimating];
}
});
}];
}
Run Code Online (Sandbox Code Playgroud)
我知道self块内部的引用导致UIViewController实例被块保留.只要performAsyncNetworkCallWithCompletion不将块存储在我的属性(或ivar)中NetworkService,我是否认为没有保留周期?
我意识到上面的这个结构将导致UIViewController被保留直到performAsyncNetworkCallWithCompletion完成,即使它是由系统早先发布的.但它可能(甚至可能吗?),系统会收回我的UIViewController 所有(更改到iOS 6的一个管理方式后UIViewController的后盾CALayer内存)?
如果有理由我必须做"弱自我/强自我舞蹈",它看起来像这样:
- (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];
}
});
}];
}
Run Code Online (Sandbox Code Playgroud)
但是我觉得这很难看,并且如果没有必要就想避免它.
objective-c uiviewcontroller ios objective-c-blocks retain-cycle