我编写了以下代码以使用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];
请告诉我的代码中的错误.