如何在后台线程中查看通知和进程?
答
最简单的方法可能是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 */
}
}
感谢您的帮助。我认为这是朝着正确方向迈出的一步,然而,从我通过实现这些方法可以知道的信息来看,它们仍然需要主要(或者实现它们的任何线程)被解锁以处理通知并分派后台线程。我宁愿在通常运行的后台线程中同时执行通知监视和处理。 – ambientdiscourse 2011-01-19 23:31:38