NSNotificationQueueを使って合体するコードを書いてみました。イベントが複数回発生しても通知を1つだけ投稿したい。
- (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 に追加された通知の数とまったく同じです。合体が機能していないと思います。
私にとって、コードの実行はこれらのケースの両方で同じままです:
ケース 1: [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"async000" object:self] postedStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
ケース 2: [[NSNotificationQueue defaultQueue] enqueueNotification: [NSNotification notificationWithName:@"async000" object:self] postedStyle:NSPostWhenIdle];
私のコードのエラーを教えてください。