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种机制,没什么关系,不要混淆!

  • 相关阅读:
    工作的价值
    面对伤害该不该回击
    建议
    利用私有的API获得手机上所安装的所有应用信息(包括版本,名称,bundleID,类型)
    你必须知道的HTTP错误
    静态库制作
    MDM证书申请的流程
    IOS客户端实现RSA加密
    获得appstore里面app的最新的版本信息,进行版本更新
    C语言实现简单php自定义扩展
  • 原文地址:https://www.cnblogs.com/breezemist/p/4933486.html
Copyright © 2011-2022 走看看