2011-01-19 120 views

回答

0

最簡單的方法可能是NSNotificationCenter的-addObserverForName:object:queue:usingBlock。我會先嚐試,但我相信有一些問題,所以這裏有一個替代方案,如果它不符合您的需求:

... 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processNotificationInBackground:) name:TheNameOfTheNotification object:nil]; 
... 

- (void) processNotificationInBackground:(NSNotification *)not { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     /* your background notification processing code goes here */ 
     /* note that you should transfer control back to the main queue if you need to update your UI */ 
    } 
} 
+0

感謝您的幫助。我認爲這是朝着正確方向邁出的一步,然而,從我通過實現這些方法可以知道的信息來看,它們仍然需要主要(或者實現它們的任何線程)被解鎖以處理通知並分派後臺線程。我寧願在通常運行的後臺線程中同時執行通知監視和處理。 – ambientdiscourse 2011-01-19 23:31:38