首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法删除NSNotificationCenter

无法删除NSNotificationCenter
EN

Stack Overflow用户
提问于 2015-11-09 14:03:14
回答 3查看 555关注 0票数 1

我使用2 NSNotificationCenter,为了避免保留周期,我创建了如下所示的weakSelf:__unsafe_unretained AugmentedRealityViewController *weakSelf = self;

代码语言:javascript
复制
    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {


        if (weakSelf.architectWorldNavigation.wasInterrupted) {
            [weakSelf.architectView reloadArchitectWorld];
        }

        [weakSelf startWikitudeSDKRendering];
    }];
    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

        [weakSelf stopWikitudeSDKRendering];
    }];

问题是,即使我在dealloc中删除了它们,它们仍然会被解雇,应用程序也会崩溃。

dealloc内部,为了删除NSNotificationCenter,我使用以下代码:

代码语言:javascript
复制
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [self.architectView removeFromSuperview];
}

当我离开ViewController时会调用它,但是通知仍然有效。有人能告诉我代码有什么问题吗?如何解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-11-09 14:56:00

通常,您最好选择处理通知的选择器方法。当然,使用基于块的API有很好的理由,但您的情况似乎是为另一种方式量身定制的。

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter]
    addObserver:self
       selector:@selector(applicationDidBecomeActiveNotification:)
           name:UIApplicationDidBecomeActiveNotification
         object:nil];

[[NSNotificationCenter defaultCenter]
    addObserver:self
       selector:@selector(applicationWillResignActiveNotification:)
           name:UIApplicationWillResignActiveNotification
         object:nil];

- (void)applicationDidBecomeActiveNotification:(NSNotification*)notification {
    if (self.architectWorldNavigation.wasInterrupted) {
        [self.architectView reloadArchitectWorld];
    }
    [self startWikitudeSDKRendering];
}

- (void)applicationWillResignActiveNotification:(NSNotification*)notification {
    [self stopWikitudeSDKRendering];
}

然后,您可以简单地删除自己,因为您是实际的观察者。

代码语言:javascript
复制
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
票数 3
EN

Stack Overflow用户

发布于 2015-11-09 14:34:24

1)删除__unsafe_unretained属性。它可能没有伤害,但它没有做任何有用的事情。

2)您没有从[NSNotificationCenter addObserverForName:object:queue:usingBlock:]获取返回值,这意味着您无法注销通知。此方法创建一个侦听通知并运行该块的代理对象。如果在某一时刻不保留引用和注销注册,则会出现内存泄漏和不需要的通知处理程序。

票数 1
EN

Stack Overflow用户

发布于 2015-11-09 14:36:03

这是因为您没有将self添加到NSNotificationCenter,而是在添加block,然后尝试删除self

当您在NSNotificationCenter中添加块时,它实际上返回一个对象,您以后可以使用该对象从NotificationCenter中删除此代码。

代码语言:javascript
复制
__unsafe_unretained AugmentedRealityViewController *weakSelf = self;

self.becomeActiveHandler = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    if (weakSelf.architectWorldNavigation.wasInterrupted) {
        [weakSelf.architectView reloadArchitectWorld];
    }
    [weakSelf startWikitudeSDKRendering];
}];

self.willResignActiveHandler = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    [weakSelf stopWikitudeSDKRendering];
}];

然后在dealloc中,您可以删除这些处理程序。

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] removeObserver:self.becomeActiveHandler];
[[NSNotificationCenter defaultCenter] removeObserver:self.willResignActiveHandler];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33610872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档