我试图在短时间内合并NSNotifications
。我尝试了以下方法:
[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self]postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
看起来我的通知很好地合并,但只有在发生UI交互时才会发送它们。例如,我将许多通知排入队列,但只有当我触摸当前视图控制器的tableView时才会触发它们。即使没有UI交互,如何解雇它们?
修改
我尝试了不同的发布方式:NSPostNow(或NSPostASAP)没有做我需要的(我的通知没有合并)
答案 0 :(得分:1)
参见编辑
我没有使用此机制的经验(感谢让我发现它;)),但我认为您正在寻找发布样式NSPostNow
。
来自the docs:
<强> NSPostNow 强>
合并后立即发布通知。
所以你的代码可能如下所示:
[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self] postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
编辑:在更详尽地阅读文档后(特别是NotificationQueues和RunLoopManagement),我认为您的问题是您只在默认运行循环中发布通知模式。
根据我的理解和我所做的测试,您应该使用发布样式 NSPostingASAP
(NSPostingNow
实际上相当于在{postNotification
上调用NSNotificationCenter
{1}},这不是你想要的)并发布所有常见的运行循环模式(即 NSRunLoopCommonModes
)。
这将要求队列尽快发布通知,即使在NSEventTrackingRunLoopMode
循环模式(included in NSRunLoopCommonModes
)期间也是如此。
[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self] postingStyle:NSPostASAP coalesceMask:NSNotificationCoalescingOnName forModes:NSRunLoopCommonModes];