在ARC中使用NSNotificationCenter代码块方法时,不会调用控制器dealloc [英] View controller dealloc not called when using NSNotificationCenter code block method with ARC

查看:110
本文介绍了在ARC中使用NSNotificationCenter代码块方法时,不会调用控制器dealloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 -addObserverForName:object:queue:usingBlock: for NSNotificationCenter 时> -viewDidLoad:我的视图控制器的方法, -dealloc 方法最终没有被调用。

When I use -addObserverForName: object: queue: usingBlock: for NSNotificationCenter in the -viewDidLoad: method of my view controller, the -dealloc method ends up not being called.

(当我删除 -addObserverForName:object:queue:usingBlock: -dealloc再次调用。)

使用 -addObserver:selector:name:object:似乎没有这个问题。我究竟做错了什么? (我的项目正在使用ARC。)

Using -addObserver: selector: name: object: doesn't seem to have this problem. What am I doing wrong? (My project is using ARC.)

以下是我的实施示例,以防我在这里做错了什么:

Below is an example of my implementation, in case I'm doing something wrong here:

[[NSNotificationCenter defaultCenter] addObserverForName:@"Update result"
                                                  object:nil
                                                   queue:nil
                                              usingBlock:^(NSNotification *note) {
                                                  updateResult = YES;
                                              }];

提前感谢您的帮助。

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

I've tried adding the following (to no avail):

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

    if ([self isMovingFromParentViewController]) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
}


推荐答案

updateResult 是一个实例变量,它可以防止对象被该块保留而取消分配。

updateResult is an instance variable which prevents the object from being deallocated as it is retained by that block.

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

In other words, you got a retain cycle. The object retains the block and the block retains the object.

您需要创建一个弱或不安全的对该实例及其变量的引用,以消除该关系。

You will need to create a weak or unsafe_unretained reference to that instance and its variable for loosing that relationship.

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

Add the following prior to your notification block:

__unsafe_unretained YouObjectClass *weakSelf = self;

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

or (in case you are on iOS5 and above)

__weak YouObjectClass *weakSelf = self;

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

Then, within that block, reference the object via that new weak reference:

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

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

Please note that retain-cycles are not a bad thing per se. Sometimes you actually want them to happen. But those are instances where you are certain that the cycle will be broken after a specific time (e.g. Animation Blocks). The cycle is broken once the block has executed and is removed from the stack.

这篇关于在ARC中使用NSNotificationCenter代码块方法时,不会调用控制器dealloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆