[英]when ViewDidUnload and Dealloc method is called in UIVIew Controller?
[英]View controller dealloc not called when using NSNotificationCenter code block method with ARC
当我在视图控制器的-viewDidLoad:
方法中使用-addObserverForName: object: queue: usingBlock:
for NSNotificationCenter
时, -dealloc
方法最终没有被调用。
(当我删除-addObserverForName: object: queue: usingBlock:
,再次调用-dealloc
。)
使用-addObserver: selector: name: object:
似乎没有这个问题。 我究竟做错了什么? (我的项目是使用ARC。)
下面是我的实现示例,以防我在这里做错了:
[[NSNotificationCenter defaultCenter] addObserverForName:@"Update result"
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
updateResult = YES;
}];
在此先感谢您的帮助。
我尝试添加以下内容(无济于事):
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self isMovingFromParentViewController]) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
}
updateResult
是一个实例变量,它可以防止对象被该块保留而取消分配。
换句话说,你有一个保留周期。 对象保留块,块保留对象。
您将需要创建一个弱或不安全的对该实例的引用及其变量以释放该关系。
在通知块之前添加以下内容:
__unsafe_unretained YouObjectClass *weakSelf = self;
或者(如果您使用的是iOS5及以上版本)
__weak YouObjectClass *weakSelf = self;
然后,在该块中,通过新的弱引用引用该对象:
[[NSNotificationCenter defaultCenter] addObserverForName:@"Update result"
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
weakSelf.updateResult = YES;
}];
请注意,保留周期本身并不是一件坏事。 有时你真的希望它们发生。 但是那些是你确定循环将在特定时间后被破坏的情况(例如动画块)。 一旦块执行并且从堆栈中移除,循环就会中断。
这很可能是因为您有一个保留周期。
当您的块隐式保留self时,通常就是这种情况,并且self会以某种方式保留块。 你将有一个保留周期,因为每个保留周期,因此他们的retainCount永远不会达到零。
您应该激活警告-Warc-retain-cycles
,它会警告您这些问题。
所以在你的情况下,你使用变量updateResult
,我假设它是一个实例变量,这隐式保留self
。 您应该使用临时弱变量来表示self,并在块中使用它,这样它就不会被保留并且您会中断保留周期。
__block __weak typeof(self) weakSelf = self; // weak reference to self, unretained by the block
[[NSNotificationCenter defaultCenter] addObserverForName:@"Update result"
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
// Use weakSelf explicitly to avoid the implicit usage of self and thus the retain cycle
weakSelf->updateResult = YES;
}];
这不是保留周期。
NSNotificationCenter
持有块,块self
。 因为[NSNotificationCenter defaultCenter]
是一个生活在所有应用程序生命周期中的单例,所以它保持self
间接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.