簡體   English   中英

多線程iOS中的通知

[英]Notifications in Multiple Thread iOS

我發現當我要將通知傳遞到特定線程時,有一個來自Apple的示例。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Threading.html#//apple_ref/doc/uid/20001289-CEGJFDFG

在本文中,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;
});

簡單的答案是:沒有區別

我還注意到蘋果建議的鏈接已更新:2009-08-18。 它似乎已經過時了。 GCD是實現多線程工作的更強大,更便捷的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM