Consolidation using NSNotificationQueue

I wrote the following code to perform a call using NSNotificationQueue. I want to publish only one notification, even if the event occurs several 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");
}

Each time the "test000AsyncTesting" method is called, notifications with the same name are added to the queue. In accordance with the concept of unification, if the queue has any number of notifications, but with the same name, then it will be placed only once. But when I run my code, "async000:" is called several times, which is exactly the number of notifications added to NSNotificationQueue. I think the union is not working.
For me, code execution remains the same in both cases:

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

2: [[NSNotificationQueue defaultQueue] enqueueNotification: [ NSNotificationWithName: @ "async000" object: self] postingStyle: NSPostWhenIdle];

, .

+1
2

Coalescing , , . , .

, test000AsyncTesting :

[[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 , .

coalesceMask NSNotificationNoCoalescing, 2 async000 .

+6

" " ; :

-(void)dealloc
{    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"async000" object:nil];

    [super dealloc];
}
0

Source: https://habr.com/ru/post/1778888/


All Articles