use*_*555 1 iphone objective-c coalescing
我编写了以下代码以使用NSNotificationQueue执行合并.我想发布一个通知,即使事件多次发生.
- (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");
}
Run Code Online (Sandbox Code Playgroud)
每次调用方法'test000AsyncTesting'时,具有相同名称的通知都会添加到队列中.根据合并的概念,如果队列具有任意数量的通知但具有相同的名称,那么它将仅发布一次.但是当我运行我的代码时,'async000:'被多次调用,这与添加到NSNotificationQueue的通知数完全相同.我认为合并不起作用.
对我来说,在这两种情况下,代码的执行都是相同的:
情况1:[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000"object:self] postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
情况2:[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000"object:self] postingStyle:NSPostWhenIdle];
请告诉我的代码中的错误.
合并仅结合在控制流返回到运行循环之前发生的通知.如果您通过运行循环将后续行程中的通知排入队列,则会导致单独的通知调用.
要查看此内容,请将test000AsyncTesting更改为将2个通知排入队列,如下所示:
[[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];
Run Code Online (Sandbox Code Playgroud)
然后async000只会在轮询时调用一次.
要进行测试,请将coalesceMask更改为NSNotificationNoCoalescing,然后在轮询时您将看到2次调用async000.