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

  • 相关阅读:
    EF Core1.0 CodeFirst为Modell设置默认值!
    MvcPager分页控件使用注意事项!
    一个关于A标签和分页的怪问题!
    让Visual Studio Code对jQuery支持智能提示!
    MVC中获取所有按钮,并绑定事件!
    EF6.0 Code First使用mysql的各种错误和解决办法!!
    记住 MVC里用formcollection接收form表单传来的值,表单属性必须有name为健!
    Hibernate处理oracle lob总结
    怎样写 OpenStack Neutron 的 Extension (一)
    怎样写 OpenStack Neutron 的 Extension (二)
  • 原文地址:https://www.cnblogs.com/breezemist/p/4933486.html
Copyright © 2011-2022 走看看