2011-01-12 3 views
1

NSNotificationQueue를 사용하여 병합을 수행하는 다음 코드를 작성했습니다. 이벤트가 여러 번 발생하더라도 하나의 알림 만 게시하고 싶습니다.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"); 
} 

'test000AsyncTesting'메서드를 호출 할 때마다 동일한 이름의 알림이 대기열에 추가됩니다. 통합의 개념에 따라 대기열에 여러 개의 알림이 있지만 이름이 같은 경우에는 한 번만 게시됩니다. 그러나 코드를 실행하면 'async000 :'이 NSNotificationQueue에 추가 된 알림 수와 정확히 동일한 여러 번 호출됩니다. 병합이 작동하지 않는다고 생각합니다. 나를 위해
은, 코드의 실행은 두 경우 모두 동일하게 유지 :

사례 1 : [[NSNotificationQueue defaultQueue] enqueueNotification : [NSNotification notificationWithName : @ "async000"개체 : 자기] postingStyle : NSPostWhenIdle coalesceMask : NSNotificationCoalescingOnName forModes : nil];

케이스 2 : [NSNotificationQueue defaultQueue] enqueueNotification : [NSNotification notificationWithName @ "async000"개체 : 자기] postingStyle : NSPostWhenIdle];

제 코드에서 오류를 알려주십시오.

답변

6

통합은 제어 흐름이 실행 루프로 돌아올 때까지 발생하는 통보 만 결합합니다. 실행 루프를 통해 후속 여행에서 알림을 대기열에 추가하면 별도의 알림 호출이 발생합니다.

은 다음과 같이 2 알림을 대기열에 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으로 변경하면 polls 할 때 async000에 2 번의 호출이 표시됩니다.

0

알림을 "등록 취소"해야합니다. 이 시도 : 거기

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

    [super dealloc]; 
} 
+0

시계, 당신은 항상'super' 내부''에 dealloc' 호출해야합니다 - (무효)의 dealloc을;' – Daniel

관련 문제