OGeek|极客世界-中国程序员成长平台

标题: ios - 合并同名的 NSNotification [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:56
标题: ios - 合并同名的 NSNotification

我正在尝试合并在短时间内发生的 NSNotifications。我尝试了以下方法:

[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self]postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];

看起来我的 通知 很好地合并了,但它们仅在 UI 交互发生时发送。例如,我将许多 通知 排入队列,但它们仅在我触摸当前 View Controller 的 tableView 时才会触发。即使没有 UI 交互,如何触发它们?

编辑
我尝试了不同的发布方式:NSPostNow(或 NSPostASAP)没有做我需要的(我的通知没有合并)



Best Answer-推荐答案


查看修改

我对这种机制没有经验(感谢让我发现它;)),但我认为您正在寻找发布风格 NSPostNow

来自 the docs :

NSPostNow

The notification is posted immediately after coalescing.

所以您的代码可能如下所示:

[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self] postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];

编辑:更彻底地阅读文档(尤其是 NotificationQueuesRunLoopManagement )后,我认为您的问题是您仅在默认运行循环模式下发布通知。 p>

根据我的理解和我做的测试,你应该使用发布风格NSPostingASAP(NSPostingNow实际上相当于调用postNotificationNSNotificationCenter 上,这不是你想要的)并发布所有常见的运行循环模式(即。NSRunLoopCommonModes )。

这将要求队列尽快发布通知,即使在 NSEventTrackingRunLoopMode 循环模式 (included in NSRunLoopCommonModes ) 期间也是如此。

[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self] postingStyle:NSPostASAP coalesceMask:NSNotificationCoalescingOnName forModes:NSRunLoopCommonModes];

关于ios - 合并同名的 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17876730/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4