iOS发送通知

2018-07-11  本文已影响0人  delims

最近看SDWebImage的源码,发现发送通知都是切换到主线程中发送。

    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
        [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:self];
    });

以往开发中一直认为在主线程和子线程中发送通知都是一样的,因为只是发送通知并没有更新UI,但这是有名的SDWebImage第三方库,不可能无缘无故的切换到主线程发通知。我猜想可能是接收通知后的操作和发送通知的操作在同一线程里,通过查询找到这样一句话

In a multithreaded application, notifications are always delivered in
the thread in which the notification was posted, which may not be the
same thread in which an observer registered itself.

所以也就是说,在哪个线程里发送,就在那个线程里执行接收通知后的操作。

上一篇下一篇

猜你喜欢

热点阅读