本文介绍了使用NSNotificationQueue时合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码以使用NSNotificationQueue执行合并.即使事件多次发生,我也只想发布一个通知.

I wrote the following code to perform coalescing using NSNotificationQueue.I want to post only one notification even if the event occurs multiple times.

- (void) test000AsyncTesting
{
    [NSRunLoop currentRunLoop];
    [[NSNotificationCenter defaultCenter] addObserver:self             selector:@selector(async000:) name:@"async000" object:self];
    [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000" object:self]
    postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];

    while (i<2)
    {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
        NSLog(@"Polling...");
        i++;
    }
}

- (void) async000:(NSNotification*)notification;
{
    NSLog(@"NSNotificationQueue");
}

每次调用方法"test000AsyncTesting"时,具有相同名称的通知将添加到队列中.根据合并的概念,如果队列有任意数量的通知但名称相同,则仅发布一次.但是当我运行代码时,多次调用"async000:",它等于添加到NSNotificationQueue的通知的数量.我认为合并不起作用.
对我来说,在这两种情况下,代码执行都保持不变:

Everytime on calling the method 'test000AsyncTesting',the notifications with same name are added to the queue.As per the concept of coalescing,if queue has any number of notifications but with same name then it will be posted only once.But when I run my code,'async000:'is called multiple times which is exactly equal to the number of notifications added to the NSNotificationQueue.I think the coalescing is not working.
For me, execution of code remains same in both of these cases:

情况1:[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000" object:self] postingStyle:NSPostWhenIdle合并掩码:NSNotificationCoalescingOnName forModes:无];

Case 1:[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000" object:self] postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];

情况2:[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000"对象:自身]postsStyle:NSPostWhenIdle];

Case 2:[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000" object:self]postingStyle:NSPostWhenIdle];

请在我的代码中告诉错误.

Please tell the error in my code.

推荐答案

Coalescing仅合并发生的通知,直到控制流返回运行循环为止.如果您在随后的运行循环中将通知加入队列,则将导致单独的通知调用.

Coalescing only combines notifications that occur until control flow returns to the run loop. If you enqueue a notification on a subsequent trip through the run loop, that will result in a separate notification call.

要查看此信息,请将test000AsyncTesting更改为使2条这样的通知入队:

To see this, change test000AsyncTesting to enqueue 2 notifications like this:

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

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

然后async000只会在轮询时被调用一次.

Then async000 will only be called once when it polls.

为了进行测试,请将CoalesceMask更改为NSNotificationNoCoalescing,然后在轮询时将看到2个对async000的调用.

For testing, change the coalesceMask to be NSNotificationNoCoalescing then you will see 2 calls to async000 when polls.

这篇关于使用NSNotificationQueue时合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:08