ios NSNotificationCenter 收到通知后的执行线程
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Threading.html#//apple_ref/doc/uid/20001289-CEGJFDFG
Delivering Notifications To Particular Threads
Regular notification centers deliver notifications on the thread in which the notification was posted. Distributed notification centers deliver notifications on the main thread. At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. For example, if an object running in a background thread is listening for notifications from the user interface, such as a window closing, you would like to receive the notifications in the background thread instead of the main thread. In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread.
One way to redirect notifications is to use a custom notification queue (not an NSNotificationQueue object) to hold any notifications that are received on incorrect threads and then process them on the correct thread. This technique works as follows. You register for a notification normally. When a notification arrives, you test whether the current thread is the thread that should handle the notification. If it is the wrong thread, you store the notification in a queue and then send a signal to the correct thread, indicating that a notification needs processing. The other thread receives the signal, removes the notification from the queue, and processes the notification.
主要内容说的是,通常情况下, 在哪个线程用 NSNotificationCenter 发出消息,就会在哪个线程里调用消息。如果想打破这个规定,就要用自定义的NSNotificationQueue去处理逻辑。
我们现在来实验一下,看看实际情况,看代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];
NSLog(@"end........");
});
}
- (void)test{
for(int i = 0;i<10000;i++){
NSLog(@"1111");
}
NSLog(@"current thread is %@",[NSThread currentThread]);
}
log输入如下:
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.232 aaaa[913:19814] 1111
2015-11-26 13:23:35.240 aaaa[913:19814] 1111
2015-11-26 13:23:35.240 aaaa[913:19814] current thread is <NSThread: 0x79f96a30>{number = 3, name = (null)}
2015-11-26 13:23:35.240 aaaa[913:19814] end........
的确是在发出的线程执行的代码。
注意 end....的输出位置,是在整个test方法执行完毕之后的!postNotification 触发的方法竟然是同步阻塞的,这更好地解释了“在哪个线程发出,就在哪个线程调用”这句话!和runloop是2种机制,没什么关系,不要混淆!
ios NSNotificationCenter 收到通知后的执行线程的更多相关文章
- IOS NSNotificationCenter(通知 的使用)监听文本框的文字改变
监听文本框的文字改变 * 一个文本输入框的文字发生改变时,文本输入框会发出一个UITextFieldTextDidChangeNotification通知 * 因此通过监听通知来监听文本输入框的文字改 ...
- iOS开发之通知中心(NSNotificationCenter)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
- 【iOS系列】- 通知NSNotification的使用
[iOS系列]- 通知NSNotification的使用 1:属性 通知属性: - (NSString *)name; // 通知的名称 - (id)object; // 通知发布者(是谁要发布通知) ...
- iOS推送通知
推送通知 此通知非彼通知. NSNotification是抽象的,看不见的,但是可以监听,属于观察者模式的一种设计模式. 推送通知是可见的,能用肉眼看见的,是真正的和用户打交道的通知. 推送通知分为两 ...
- iOS开发之通知使用总结
通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ...
- Swift - 使用NSNotificationCenter发送通知,接收通知
转载自:http://www.mamicode.com/info-detail-1069228.html 标签: 1,通知(NSNotification)介绍 这里所说的通知不是指发给用户看的通知消息 ...
- ios开发之通知事件
每天学习一点点,总结一点点,成功从良好的习惯开始! 昨天学习了ios开发中的关于通知事件的一些东西,在这里简单总结下,仅供初学者学习,更多的是怕我自己忘了,咩哈哈~~~~ 通知(notificatio ...
- iOS开发之通知机制
1.通知中心 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification), ...
- IOS中的通知NSNotification
类似于Android中的广播接收者发送者 1.一般完整的通知包含三个属性 -(NSString *)name ;//通知的名称 -(id)object ;//通知发布者(是谁要发布通知) -(NSDi ...
随机推荐
- html5入门
1.canvas标签 <canvas id="myCanvas"></canvas><!--canvas标签定义图形,比如图标和其他图像--> ...
- Xshell中文版使用说明/下载
个人本来一直用SecureCRT的这几天朋友推荐用xshell就尝试下载了一个结果发现这个比SecureCRT和puttytel好使多了切更能更完善强大特此推荐附带注册码一枚可装999台机器 1309 ...
- [Angularjs]表单验证
写在前面 在开发中提交表单,并对表单的值进行验证是非常常见的操作,angularjs对表单验证提供了非常好的支持. demo 表单 <form name="myform" n ...
- c语言中time相关函数
工作中遇到的函数: int seed = time(NULL); srand(seed); signal(SIGINT, stop); signal(SIGUSR1, sig_usr1); 搜time ...
- golang笔记——数据类型
25个关键字 程序声明:import, package 程序实体声明和定义:chan, const, func, interface, map, struct, type, var 程序流程控制:go ...
- solr6.1-----solrJ 程序管理索引库
solrJ 是solr 提供的一个客户端,就是一个jar 包,把jar 添加到工程中整合solr 服务. 所需jar 包 D:\solr-6.1.0\dist 下面的 solr-solrj-6.1.0 ...
- [设计模式] javascript 之 责任链模式
责任链模式:定义 责任链接模式又称职责链模式,是一种对象的行为模式:它是一种链式结构,每个节点都有可能两种操作,要么处理该请求停止该请求操作,要么把请求转发到下一个节点,让下一个节点来处理请求:该模式 ...
- Linux运维初级教程(三)文件及目录权限
文件类型 -代表普通文件,d代表目录,l代表链接文件,b或c代表设备. 第二至九个字符代表权限,分别为所有者权限.所属组权限.其他账户权限 修改权限用chmod u用户 g组 o其他用户 a所有人 c ...
- 2015年12月10日 spring初级知识讲解(三)Spring消息之activeMQ消息队列
基础 JMS消息 一.下载ActiveMQ并安装 地址:http://activemq.apache.org/ 最新版本:5.13.0 下载完后解压缩到本地硬盘中,解压目录中activemq-core ...
- php缓存技术总结
缓存是指临时文件交换区,电脑把最常用的文件从存储器里提出来临时放在缓存里,就像把工具和材料搬上工作台一样,这样会比用时现去仓库取更方便.因为缓存往往使用的是RAM(断电即掉的非永久储存), ...