سؤال

I have this NSTimer that I want it to stop after 45 minutes, but it won't stop. What am I doing wrong?

TIMER_COUNT = 45

HOURS_IN_HOURS = 60

HOURS_IN_DAY = 24

- (void)start
{
    self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}

- (void)stop
{
    [self.timerCountdown invalidate];
    self.timerCountdown = nil;
}

- (void)updateCountdown
{
    NSDate *currentDate = [NSDate date];
    NSDate *finalTime = [currentDate dateByAddingTimeInterval:(TIMER_COUNT * HOURS_IN_HOUR)];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *componentsHours   = [calendar components:NSHourCalendarUnit fromDate:currentDate];
    NSDateComponents *componentMinuts   = [calendar components:NSMinuteCalendarUnit fromDate:currentDate];
    NSDateComponents *componentSeconds  = [calendar components:NSSecondCalendarUnit fromDate:currentDate];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *componentsDaysDiff = [gregorianCalendar components:NSDayCalendarUnit
                                                                fromDate:currentDate
                                                                  toDate:finalTime
                                                                 options:0];

    NSLog(@"%20d Days, %02d Hours, %02d Minutes, %02d Seconds.", componentsDaysDiff.day, HOURS_IN_DAY - componentsHours.hour, HOURS_IN_HOUR - componentMinuts.minute, HOURS_IN_HOUR - componentSeconds.second);

    if ([currentDate compare:finalTime] == NSOrderedSame)
    {
        NSLog(@"Done.");
        [self stop];
    }
}

Thanks in advance.

هل كانت مفيدة؟

المحلول

Because your currentDate will keep being set every time your timer ticks. [NSDate date] will set currentDate to the current time every time the updateCountdown method runs. Therefore will finalTime always be 45 minutes ahead of currentDate. You should create a startDate property and set it in the start method instead:

- (void)start
{
    self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
    self.startDate = [NSDate date];
}

Then check on the property in the updateCountdown method:

if ([self.startDate compare:finalTime] == NSOrderedSame)
{
    NSLog(@"Done.");
    [self stop];
}

Alternatively you can use an integer with the number of ticks you are expecting and then substract one from the integer everytime the timer ticks. Something like this:

- (void)start
{
    self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES];
    self.countdown = TIMER_COUNT * HOURS_IN_HOUR;
}

- (void)updateCountdown
{
    self.countdown--;

    //your code

    if (self.countdown == 0)
    {
         NSLog(@"Done.");
         [self stop];
    }
}

نصائح أخرى

- (void)start {
   NSString *startTime = [NSDate date];

   NSDateComponents *durationComponents = [[[NSDateComponents alloc] init] autorelease];
   durationComponents.minutes = 45;

   NSCalendar *gregorianCalendar = [NSCalendar currentCalendar];

   self.endTime = [calendar dateByAddingComponents:durationComponents
                                            toDate:startTime
                                           options:0];

   self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                          target:self           
                                                        selector:@selector(updateCountdown)
                                                        userInfo:nil 
                                                         repeats:YES];
}

- (void)stop {
    [self.timerCountdown invalidate];
    self.timerCountdown = nil;
}

- (void)updateCountdown {
    NSDate *currentTime = [NSDate date];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *currentTimeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit)
                                                          fromDate:currentTime];


    if ([currentTime compare:self.endTime] == NSOrderedDescending) {
        [self stop];
    }
}

You don't need the startDate property.
And you should not check dates for equality. That is very unlikely to happen. Use that comparison:

if ([finalTime compare:[NSDate date]] == NSOrderedAscending)
{
     NSLog(@"Done.");
     [self.timerCountdown invalidate];
     [self stop];
}

This means that finalTime is earlier in time than current time.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top