• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 合并同名的 NSNotification

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:56:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试合并在短时间内发生的 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap