1

I want to check for whether the request has timed out after some period.

I know NSNotifiactionCenter ,but don't know how to set the request time out notification for it.

thanks.

4

1 回答 1

4

如果到那时还没有收到,您​​可以只使用计时器并取消您的通知请求吗?

例如,以 Apple 的可达性为例:

- (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];
    }
}
于 2009-12-14T13:10:07.277 回答