在ARC中使用NSNotificationCenter代码块方法时,不会调用控制器dealloc

Jam*_*mes 8 iphone uiviewcontroller dealloc nsnotificationcenter ios

当我使用-addObserverForName: object: queue: usingBlock:NSNotificationCenter-viewDidLoad:我的视图控制器的方法,该-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;
                                              }];
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

我尝试添加以下内容(无济于事):

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    if ([self isMovingFromParentViewController]) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
}
Run Code Online (Sandbox Code Playgroud)

Til*_*ill 17

updateResult 是一个实例变量,它可以防止对象在被该块保留时被释放.

换句话说,你有一个保留周期.对象保留块,块保留对象.

您将需要创建一个弱或不安全的对该实例的引用及其变量以释放该关系.

在通知块之前添加以下内容:

__unsafe_unretained YouObjectClass *weakSelf = self;
Run Code Online (Sandbox Code Playgroud)

或者(如果您使用的是iOS5及以上版本)

__weak YouObjectClass *weakSelf = self;
Run Code Online (Sandbox Code Playgroud)

然后,在该块中,通过新的弱引用引用该对象:

[[NSNotificationCenter defaultCenter] addObserverForName:@"Update result"
                                                  object:nil
                                                   queue:nil
                                              usingBlock:^(NSNotification *note) {
                                                  weakSelf.updateResult = YES;
                                              }];
Run Code Online (Sandbox Code Playgroud)

请注意,保留周期本身并不是一件坏事.有时你真的希望它们发生.但是那些是你确定循环将在特定时间后被破坏的情况(例如动画块).一旦块执行并且从堆栈中移除,循环就会中断.


Ali*_*are 6

这很可能是因为您有一个保留周期.

当您的块隐式保留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;
                                          }];
Run Code Online (Sandbox Code Playgroud)