我使用2 NSNotificationCenter,为了避免保留周期,我创建了如下所示的weakSelf:__unsafe_unretained AugmentedRealityViewController *weakSelf = self;
[[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,我使用以下代码:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.architectView removeFromSuperview];
}当我离开ViewController时会调用它,但是通知仍然有效。有人能告诉我代码有什么问题吗?如何解决这个问题?
发布于 2015-11-09 14:56:00
通常,您最好选择处理通知的选择器方法。当然,使用基于块的API有很好的理由,但您的情况似乎是为另一种方式量身定制的。
[[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];
}然后,您可以简单地删除自己,因为您是实际的观察者。
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}发布于 2015-11-09 14:34:24
1)删除__unsafe_unretained属性。它可能没有伤害,但它没有做任何有用的事情。
2)您没有从[NSNotificationCenter addObserverForName:object:queue:usingBlock:]获取返回值,这意味着您无法注销通知。此方法创建一个侦听通知并运行该块的代理对象。如果在某一时刻不保留引用和注销注册,则会出现内存泄漏和不需要的通知处理程序。
发布于 2015-11-09 14:36:03
这是因为您没有将self添加到NSNotificationCenter,而是在添加block,然后尝试删除self。
当您在NSNotificationCenter中添加块时,它实际上返回一个对象,您以后可以使用该对象从NotificationCenter中删除此代码。
__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中,您可以删除这些处理程序。
[[NSNotificationCenter defaultCenter] removeObserver:self.becomeActiveHandler];
[[NSNotificationCenter defaultCenter] removeObserver:self.willResignActiveHandler];https://stackoverflow.com/questions/33610872
复制相似问题