zoukankan      html  css  js  c++  java
  • 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种机制,没什么关系,不要混淆!

  • 相关阅读:
    Javascript Promise技术
    什么是CPS
    移动端input file 提示没有应用可执行此操作
    黄聪:wordpress+Windows下安装Memcached服务及安装PHP的Memcached扩展
    黄聪:Windows下安装Memcached服务及安装PHP的Memcached扩展
    用VScode配置Python开发环境
    在VSCode中使用码云(Gitee)进行代码管理
    网页链接分享到微信朋友圈带图标和摘要的完美解决方法
    解决百度统计被刷广告的办法,屏蔽非法广告
    Visual Studio代码PHP Intelephense继续显示不必要的错误
  • 原文地址:https://www.cnblogs.com/breezemist/p/4933486.html
Copyright © 2011-2022 走看看