NSNotificationCenter 中的addObserver和addObserverForName的释放问题。
来源:互联网 发布:三国杀 界张辽 知乎 编辑:程序博客网 时间:2024/05/23 18:29
起初以为addObserverForName的释放问题和addObserver的释放是一样。测试结果是不同的。
先说下addObserver的一般处理步骤:
1.加入观察者
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealWithNote:) name:@"NoteName" object:nil];
2.实现响应
-(void)dealWithNote:(NSNotification *)note {
}
3.推送消息
[[NSNotificationCenter defaultCenter] postNotificationName:@"NoteName" object:nil];
4.释放资源
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NoteName" object:nil];
而addObserverForName就有些不同了。
1.注册观察者,加入了queue和__block. 这样简化了代码。 例如:
_observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"NoteName"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
}];
4.释放资源
// 这里不能使用
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NoteName" object:nil];
而使用一下方式
if(_observer){
[[NSNotificationCenter defaultCenter] removeObserver:_observer];
}
- NSNotificationCenter 中的addObserver和addObserverForName的释放问题。
- NSNotificationCenter 中的addObserver和addObserverForName的释放问题。
- NSNotificationCenter addObserverForName 的释放
- NSNotificationCenter中的addObserverForName
- KVO 和 NSNotificationcenter addObserver 和removeObserve成对出现的原因
- [NSNotificationCenter defaultCenter] addObserver 时 object的意义。
- iOS: NSNotificationCenter的方法addObserverForName:object:queue:usingBlock:
- objective-c NSNotificationCenter,addObserver
- iOS: NSNotificationCenter的方法addObserver:selector:name:object:
- 关于[[NSNotificationCenter defaultCenter] addObserver不remove后续又收到通知crash问题
- addObserver和postNotification函数传递函数的区别
- NSNotificationCenter的一个问题
- iphone 消息处理事件 postNotificationName和addObserver的调用
- iphone 消息处理事件 postNotificationName和addObserver的调用
- addObserver
- NSNotificationCenter同步异步的问题
- NSNotificationCenter 释放的两种方法的利弊
- NSNotificationCenter的同步和异步
- Android之项目推荐使用的第三方库,有助于快速开发,欢迎各位网友补充
- 九度_题目1370:数组中出现次数超过一半的数字
- GDI+ /GDI Plus 绘制圆角矩形 Draw retangle round
- Android Widget桌面组件教程整理(二)
- java 线程状态流转
- NSNotificationCenter 中的addObserver和addObserverForName的释放问题。
- JAVA远程下载zip包并解压到指定目录
- wince 之在dos下生成nk
- iOS 开发值得拥有的75 个工具
- android 加密
- Java 集合类
- Shell 命令--echo tr ls--总结自《Linux Shell 脚本攻略》
- Linux文件虚拟机系统只读Read-only file system的快速解决方法
- Java面试题七(集合专题)