创建一个计时器以超时并弹出警报

发布于 2024-12-04 01:37:44 字数 1250 浏览 2 评论 0原文

我正在尝试使用 CLLocationManger 获取最近的位置。如果我无法在 30 秒内收到邮件,我希望弹出一个警报,提示“我们现在遇到问题”或类似的内容。在我的 CLLocationManager 委托中,我执行以下操作:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSDate *eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    NSTimer *timer;
    if (abs(howRecent) < 1.0) {     // process time if time is less than 1.0 seconds old.
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCount:) userInfo:nil repeats:NO];  
        return;
    }

我的 updateCount 方法:

- (void)updateCount:(NSTimer *)aTimer {
    count++;
    NSLog(@"%i", count);
    if (count == 30) {
        [aTimer invalidate];
        NSLog(@"Timer invalidated");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Down" message:@"Sorry, but we cannot find your location at this time.  Please try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

但我没有看到我的警报被触发。我尝试使用重复计时器,但在达到 30 秒并显示警报后,它会再次重复。所以我不太确定我在这里做错了什么。谢谢!

I'm trying to get a recent location using CLLocationManger. If I cannot get one within 30 seconds, I want an alert to pop up saying that "we're having problems right now" or something along those lines. In my CLLocationManager Delegate, I do this:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSDate *eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    NSTimer *timer;
    if (abs(howRecent) < 1.0) {     // process time if time is less than 1.0 seconds old.
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCount:) userInfo:nil repeats:NO];  
        return;
    }

My updateCount method:

- (void)updateCount:(NSTimer *)aTimer {
    count++;
    NSLog(@"%i", count);
    if (count == 30) {
        [aTimer invalidate];
        NSLog(@"Timer invalidated");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Down" message:@"Sorry, but we cannot find your location at this time.  Please try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

But I don't see my alert get fired. I tried with a repeating timer, but after it hits 30 seconds, and the alert is shown, it repeats again. So I'm not quite sure what I'm doing wrong here. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云不回头 2024-12-11 01:37:44

只是对 updateCount 方法的一个小更新:

- (void)updateCount:(NSTimer *)aTimer { 
    count++; 
    NSLog(@"%i", count); 
    if (count == 30) { 
        [aTimer invalidate]; 
        NSLog(@"Timer invalidated"); 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Down" message:@"Sorry, but we cannot find your location at this time.  Please try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
        [alert show]; 
        [alert release];
        [timer invalidate];
        timer = nil;
    } 
} 

不要在 locationManager 方法中使用 NSTimer *timer; 。相反,直接调用timer = [[NSTimer....]]方法,我认为因为updateCount方法中有一个参数,即NSTimer,并且我们使用不同的NSTimer实例,所以这里基本上是问题。尝试在 updateCount 方法中不使用参数并使 .h 文件中声明的计时器无效。

just a small update to updateCount method :

- (void)updateCount:(NSTimer *)aTimer { 
    count++; 
    NSLog(@"%i", count); 
    if (count == 30) { 
        [aTimer invalidate]; 
        NSLog(@"Timer invalidated"); 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Down" message:@"Sorry, but we cannot find your location at this time.  Please try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
        [alert show]; 
        [alert release];
        [timer invalidate];
        timer = nil;
    } 
} 

Don't use NSTimer *timer; in locationManager method. instead directly call timer = [[NSTimer....]] method and I think because there is one argument in updateCount method i.e. NSTimer and we are using different NSTimer instance so here is basically problem. Try without using argument in updateCount Method and invalidate the timer that is declared in .h file.

横笛休吹塞上声 2024-12-11 01:37:44

我检查了文档并发现了以下段落。

但是,对于重复计时器,您必须通过调用计时器对象的 invalidate 方法自行使其无效。调用此方法请求从当前运行循环中删除计时器;因此,您应该始终从安装计时器的同一线程调用 invalidate 方法。

因此,要使计时器无效,请尝试调用 PerformSelector。

我不确定它是否会有所帮助,但如果它有效,请在此处发布,以便其他人也可以利用。

I checked the documentation and found following paragraph in that.

However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed.

So, for invalidating timer try calling performSelector.

I am not sure weather it will help or not but if it works then please post here so that other too can take advantage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文