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 收到通知后的执行线程的更多相关文章

  1. IOS NSNotificationCenter(通知 的使用)监听文本框的文字改变

    监听文本框的文字改变 * 一个文本输入框的文字发生改变时,文本输入框会发出一个UITextFieldTextDidChangeNotification通知 * 因此通过监听通知来监听文本输入框的文字改 ...

  2. iOS开发之通知中心(NSNotificationCenter)

    前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...

  3. 【iOS系列】- 通知NSNotification的使用

    [iOS系列]- 通知NSNotification的使用 1:属性 通知属性: - (NSString *)name; // 通知的名称 - (id)object; // 通知发布者(是谁要发布通知) ...

  4. iOS推送通知

    推送通知 此通知非彼通知. NSNotification是抽象的,看不见的,但是可以监听,属于观察者模式的一种设计模式. 推送通知是可见的,能用肉眼看见的,是真正的和用户打交道的通知. 推送通知分为两 ...

  5. iOS开发之通知使用总结

    通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ...

  6. Swift - 使用NSNotificationCenter发送通知,接收通知

    转载自:http://www.mamicode.com/info-detail-1069228.html 标签: 1,通知(NSNotification)介绍 这里所说的通知不是指发给用户看的通知消息 ...

  7. ios开发之通知事件

    每天学习一点点,总结一点点,成功从良好的习惯开始! 昨天学习了ios开发中的关于通知事件的一些东西,在这里简单总结下,仅供初学者学习,更多的是怕我自己忘了,咩哈哈~~~~ 通知(notificatio ...

  8. iOS开发之通知机制

    1.通知中心 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification), ...

  9. IOS中的通知NSNotification

    类似于Android中的广播接收者发送者 1.一般完整的通知包含三个属性 -(NSString *)name ;//通知的名称 -(id)object ;//通知发布者(是谁要发布通知) -(NSDi ...

随机推荐

  1. 关于hibernate纯sql查询返回结果集的问题(hbm.xml中不写多表关联)

    相信用过hibernate的兄弟们都会因为多表复杂查询后,为返回的结果如何组装到一个VO中而烦恼不已.我也不停的为此而烦恼,但是在看了hibernate的transform后,感觉这个方法还挺管用的. ...

  2. 网络方案 & HTTP状态码

    在iOS中,常见的发送HTTP请求的方案包括: 苹果官方 名称 说明 NSURLConnection iOS 2.0 推出,用法简单,最古老最经典最直接的一种方案 NSURLSession iOS 7 ...

  3. centos yum 安装

    LINUX下YUM源配置 1.确保RHEL5中已经安装了yum [root@lvs-master ~]# rpm -qa |grep yumyum-metadata-parser-1.1.2-3.el ...

  4. 自定义MapReduce的类型

    package org.apache.hadoop.mapreduce.io; import java.io.DataInput; import java.io.DataOutput; import ...

  5. Unity API

    关于 int Mathf.PingPong(t, length); 原理,相当于 #include <iostream> #include <vector> int test( ...

  6. 编译本地64位版本的hadoop-2.6.0

     官方提供的hadoop-2.x版本貌似都是32位的,在64位机子下使用可能会报错,最好使用官方提供的源码进行本地编译,编译成适合本地硬件环境的64位软件包. 关于native  Hadoop是使用J ...

  7. JSON格式转换(javascript)

    使用ajax从后台抓取数据后,如果有多个值,可以使用json传值. ajax例子如下,在返回的类型里面,可以是文本型(text),JSON格式(json),超文本类型(html),XML文件类型(xm ...

  8. Ubuntu 14 如何打开 .chm格式文档?

    好多手册是.chm格式,Ubuntu是需要安装第三方软件才能打开.chm格式文档,操作方式如下: 到“软件中心” -> 搜索“xchm”,并安装 -> 右键某个.chm文档,选择“属性” ...

  9. [原创]C 语言select函数

    参考链接:http://www.cnblogs.com/GameDeveloper/p/3406565.html 注意点: select() 只是执行一次的超时检测.重新进行select要重新设置“超 ...

  10. 开关WIFI脚本

    title wifi管理color A@echo on@echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@echo 1.启用并设定虚拟WiFi网卡;@echo 2.开启无线网络; ...