Obj- C 为 NSDistributionNotificationCenter 配置 运行 循环
Obj- C Configure Run Loop for NSDistributionNotificationCenter
我正在为我的辅助任务制作一个简单的 IPC 模块,
我决定使用 NSDistributionNotificationCenter
因为它很简单。
但是我认为它需要 运行 在 运行 循环中,而我没有,
所以我需要创建一个 RunLoop。
我已在此处阅读有关 RunLoops 的文档 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html
但是我不清楚,我应该如何只为接收和发送做这个RunLoop Notifications
。
有人能告诉我我的 RunLoop
应该包括什么吗?
它应该包括一个计时器吗?
如何将消息侦听器附加到此 RunLoop
?
如果我的问题有点菜鸟,我很抱歉 - 我以前从来没有手动弄乱过 运行loops。
我知道我必须创建一个线程并为其分配一个 运行 循环,
但不清楚如何添加观察者?我应该添加任何其他内容吗?
其实比我想象的要容易得多
我正在回答我自己的问题,以防万一有人将来会为同样的事情而苦苦挣扎,他可能会在这里找到答案或至少是一个启动。
BOOL done = NO;
NSDistributedNotificationCenter * notificator = [NSDistributedNotificationCenter defaultCenter];
[notificator addObserver:self selector:@selector(gotObject:) name:@"com.ipc.test" object:nil];
// Add your sources or timers to the run loop and do any other setup.
do
{
// Start the run loop but return after each source is handled.
SInt32 result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, YES);
// If a source explicitly stopped the run loop, or if there are no
// sources or timers, go ahead and exit.
if ((result == kCFRunLoopRunStopped) || (result == kCFRunLoopRunFinished))
done = YES;
// Check for any other exit conditions here and set the
// done variable as needed.
}
while (!done);
实际上我现在找到了更简单的解决方案
NSDistributedNotificationCenter * notificator = [NSDistributedNotificationCenter defaultCenter];
[notificator addObserver:self selector:@selector(gotObject:) name:@"com.ipc.test" object:nil];
CFRunLoopRun();
我正在为我的辅助任务制作一个简单的 IPC 模块,
我决定使用 NSDistributionNotificationCenter
因为它很简单。
但是我认为它需要 运行 在 运行 循环中,而我没有, 所以我需要创建一个 RunLoop。
我已在此处阅读有关 RunLoops 的文档 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html
但是我不清楚,我应该如何只为接收和发送做这个RunLoop Notifications
。
有人能告诉我我的 RunLoop
应该包括什么吗?
它应该包括一个计时器吗?
如何将消息侦听器附加到此 RunLoop
?
如果我的问题有点菜鸟,我很抱歉 - 我以前从来没有手动弄乱过 运行loops。
我知道我必须创建一个线程并为其分配一个 运行 循环, 但不清楚如何添加观察者?我应该添加任何其他内容吗?
其实比我想象的要容易得多 我正在回答我自己的问题,以防万一有人将来会为同样的事情而苦苦挣扎,他可能会在这里找到答案或至少是一个启动。
BOOL done = NO;
NSDistributedNotificationCenter * notificator = [NSDistributedNotificationCenter defaultCenter];
[notificator addObserver:self selector:@selector(gotObject:) name:@"com.ipc.test" object:nil];
// Add your sources or timers to the run loop and do any other setup.
do
{
// Start the run loop but return after each source is handled.
SInt32 result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, YES);
// If a source explicitly stopped the run loop, or if there are no
// sources or timers, go ahead and exit.
if ((result == kCFRunLoopRunStopped) || (result == kCFRunLoopRunFinished))
done = YES;
// Check for any other exit conditions here and set the
// done variable as needed.
}
while (!done);
实际上我现在找到了更简单的解决方案
NSDistributedNotificationCenter * notificator = [NSDistributedNotificationCenter defaultCenter];
[notificator addObserver:self selector:@selector(gotObject:) name:@"com.ipc.test" object:nil];
CFRunLoopRun();