多线程中的通知 iOS
Notifications in Multiple Thread iOS
我发现当我想向特定线程发送通知时,Apple 提供了一个示例。
在本文中,Apple 建议使用 machport 来监视我们要处理通知的线程。
- (void)processNotification:(NSNotification *)notification {
if ([NSThread currentThread] != notificationThread) {
// Forward the notification to the correct thread.
[self.notificationLock lock];
[self.notifications addObject:notification];
[self.notificationLock unlock];
[self.notificationPort sendBeforeDate:[NSDate date]
components:nil
from:nil
reserved:0];
}
else {
// Process the notification here;
}
}
我的问题是:如果我收到通知并使用 dispatch_async
处理通知,它是否有不同的外观?
dispatch_async(dispatch_get_main_queue(), ^{
// Process the notification here;
});
简单的答案是:没有区别
我还注意到苹果建议 link 已更新:2009-08-18。它似乎已经过时了。 GCD是实现多线程工作的一种更强大、更方便的方式。
我发现当我想向特定线程发送通知时,Apple 提供了一个示例。
在本文中,Apple 建议使用 machport 来监视我们要处理通知的线程。
- (void)processNotification:(NSNotification *)notification {
if ([NSThread currentThread] != notificationThread) {
// Forward the notification to the correct thread.
[self.notificationLock lock];
[self.notifications addObject:notification];
[self.notificationLock unlock];
[self.notificationPort sendBeforeDate:[NSDate date]
components:nil
from:nil
reserved:0];
}
else {
// Process the notification here;
}
}
我的问题是:如果我收到通知并使用 dispatch_async
处理通知,它是否有不同的外观?
dispatch_async(dispatch_get_main_queue(), ^{
// Process the notification here;
});
简单的答案是:没有区别
我还注意到苹果建议 link 已更新:2009-08-18。它似乎已经过时了。 GCD是实现多线程工作的一种更强大、更方便的方式。