如何检查请求超时,是否有我可以设置的通知?

时间:2009-12-14 12:35:57

标签: iphone timeout request nsnotification

我想检查请求是否在一段时间后超时。

我知道NSNotifiactionCenter,但不知道如何设置请求超时通知。

感谢。

1 个答案:

答案 0 :(得分:4)

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

e.g。以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];
    }
}
相关问题