iOS NSNotyfication 注销须注意
最近的项目中,用到了NSNotyfication,可是发现回调函数总是重复调用。原以为viewcontroller不调用后,就自动释放了,其实还是有延时的。所以朋友们一定不要忘记removeobserver。
[[NSNotificationCenter defaultCenter] addObserverForName:@"RGIP_NeedUpdateNF_UPDATE"
object:nil queue:nil
usingBlock:^(NSNotification *note) {
NSString * name=[note object];
[self reloadRGIPGroup_add:name];
}];
使用block的NSNotifaction,不过无法通过remove注销掉
-(void) removeLister
{
NSLog(@"------------------------");
NSLog(@"remove:%@",NSStringFromClass([self class]));
self.addFlag=0;
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"RGIP_NeedUpdateNF_UPDATE" object:@"name"];
}
原因是addobserver函数的不一致的原因,我们应该使用如下函数
[[NSNotificationCenter defaultCenter] removeObserver:self name:RGIPNOTY object:nil];
另外,如果要全部注销,可以用
//[[NSNotificationCenter defaultCenter] removeObserver:self name:RGIPNOTY object:nil];
//[[NSNotificationCenter defaultCenter] removeObserver:self name:RGIPNOTY_ADD object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];