菜鸟教程小白 发表于 2022-12-12 12:56:09

ios - 合并同名的 NSNotification


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

<pre><code>[ enqueueNotification:postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
</code></pre>

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

<p><strong>编辑</strong><br/>
我尝试了不同的发布方式:NSPostNow(或 NSPostASAP)没有做我需要的(我的通知没有合并)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>查看修改</strong></p>

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

<p>来自 <a href="https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSNotificationQueue_Class/Reference/Reference.html#//apple_ref/doc/uid/20000220-DontLinkElementID_2" rel="noreferrer noopener nofollow">the docs</a> :</p>

<blockquote>
<p><strong>NSPostNow</strong></p>

<p>The notification is posted immediately after coalescing.</p>
</blockquote>

<p>所以您的代码可能如下所示:</p>

<pre><code>[ enqueueNotification: postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
</code></pre>

<p><strong>编辑</strong>:更彻底地阅读文档(尤其是 <a href="https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/Notifications/Articles/NotificationQueues.html" rel="noreferrer noopener nofollow">NotificationQueues</a> 和 <a href="https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW12" rel="noreferrer noopener nofollow">RunLoopManagement</a> )后,我认为您的问题是您仅在默认运行循环模式下发布通知。</strong> p>

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

<p>这将要求队列尽快发布通知,即使在 <code>NSEventTrackingRunLoopMode</code> 循环模式 (<a href="https://developer.apple.com/library/mac/documentation/cocoa/conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW12" rel="noreferrer noopener nofollow">included in <code>NSRunLoopCommonModes</code></a> ) 期间也是如此。</p>

<pre><code>[ enqueueNotification: postingStyle:NSPostASAP coalesceMask:NSNotificationCoalescingOnName forModes:NSRunLoopCommonModes];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 合并同名的 NSNotification,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17876730/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17876730/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 合并同名的 NSNotification