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