开发者

Creating a timer to timeout and pop up an alert

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开发者_JAVA百科 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜