2009-12-14 74 views

回答

4

你可以只使用一个计时器并取消你的通知请求,如果没有收到呢?

例如以苹果的可达性为例:

- (void) startNotifier 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil]; 
    notified = NO; 
    [self performSelector:@selector(onRequestTimeout) withObject:nil afterDelay:5.0]; // 5 secs 
} 

- (void)onReachabilityChanged:(NSNotification *)note 
{ 
     // Do whatever on notification 
     notified = YES; 
} 

- (void) onRequestTimeout 
{ 
    if (!notified) 
    { 
     // Do whatever on request timeout 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNetworkReachabilityChangedNotification" object:nil]; 
    } 
} 
相关问题