NSNotificationCenter addObserverForName 的释放

来源:互联网 发布:gpgpu编程技术 编辑:程序博客网 时间:2024/06/06 06:44
  • 先让我们来看看正确的姿势, 一定要在想要释放内存前把持有的对象释放掉, 千万不要在dealloc 里面写, 根本没有机会走到dealloc, 这个跟NSTime 类类似的道理,[self.timer invalidate]; 要提前处理掉
@property (nonatomic, strong) id observer;- (void) viewDidLoad {  self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification                                                      object:nil                                                       queue:[NSOperationQueue mainQueue]                                                  usingBlock:^(NSNotification *note) {                                                      NSLog("hello");                                                  }];}- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];        if (self.observer) {        [[NSNotificationCenter defaultCenter] removeObserver:self.observer];        self.observer = nil;    }}- (void)dealloc{    NSLog(@"MTC_dealloc OK");}

下面这个兄弟写错了 , 永远不会释放掉这个UIViewControl了……
http://www.jianshu.com/p/1788d15c570b

0 0