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");
}
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:
Case 1: [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000" object:self] postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
Case 2: [[NSNotificationQueue defaultQueue] enqueueNotification: [NSNotification notificationWithName:@"async000" object:self] postingStyle:NSPostWhenIdle];
Please tell the error in my code.