hanumanDev
hanumanDev

Reputation: 6614

NSTimer and NSDate countdown clock works in the simulator but doesn't on the device

I have a NSTimer and an NSDate working together to make a countdown clock. A UILabel is then updated with the countdown.

The problem I'm facing is that the countdown clock works in the simulator but doesn't on the device. The simulator iOS 6.0 and the device iOS 6.0.1.

here's my code:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

    [self startTimer];

}

 // timer and update label to display a countdown clock 
- (void)startTimer {

    // Create a timer that fires every second repeatedly and save it in an ivar
    NSTimer *timer = [[NSTimer alloc] init];

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

}

  - (void)updateLabel {

    // convert date string to date then set to a label
    NSDateFormatter *dateStringParser = [[NSDateFormatter alloc] init];
    [dateStringParser setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.000Z"];

    NSDate *date = [dateStringParser dateFromString:deadlineDate];

    NSLog(@"date from update label %@", date);

    NSTimeInterval timeInterval = [date timeIntervalSinceNow]; ///< Assuming this is in the future for now.

    NSString *stringVariable = [self stringFromTimeInterval:timeInterval];

    self.deadlineLbl.text = [NSString stringWithFormat:@"%@", stringVariable];

    NSLog(@"%@",stringVariable);

}

- (NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
    NSInteger ti = (NSInteger)interval;
  //  NSInteger seconds = ti % 60;
    NSInteger minutes = (ti / 60) % 60;
    NSInteger hours = (ti / 3600);
    return [NSString stringWithFormat:@"%02i hours : %02i min", hours, minutes];
}

here's some console output first on the simulator and then on the device:

simulator console output:

2012-11-14 15:39:57.021 appName[7856:13d03] 31 hours : 20 min
2012-11-14 15:39:58.020 appName[7856:13d03] 31 hours : 20 min
2012-11-14 15:39:59.021 appName[7856:13d03] 31 hours : 19 min
2012-11-14 15:40:00.021 appName[7856:13d03] 31 hours : 19 min

device console output:

2012-11-14 15:45:44.887 appName[7556:907] 00 hours : 00 min
2012-11-14 15:45:45.896 appName[7556:907] 00 hours : 00 min
2012-11-14 15:45:46.891 appName[7556:907] 00 hours : 00 min
2012-11-14 15:45:47.891 appName[7556:907] 00 hours : 00 min

another problem I'm having is that the timer keeps updating after the user leaves the view.

thanks for any help

Upvotes: 1

Views: 1917

Answers (2)

danh
danh

Reputation: 62676

None of that code looks suspicious in terms of differing behavior between simulator and hardware.

How do you initialize deadlineDate? Try to rule that out as a problem by forcing a value in start timer....

deadlineDate = [NSDate dateWithTimeIntervalSinceNow:3600];

There are a few other improvements you could make to the code (e.g. not sure what you're doing with labelFormatter in the startTimer method, could delete those lines, persisting date formatters, etc.), but nothing that would explain simulator vs. hw behavior.

Upvotes: 1

Alex
Alex

Reputation: 2468

Check for date variable in line:

NSDate *date = [dateStringParser dateFromString:deadlineDate];

Check in debugger if the date is not nil. If it is, then the problem is in the deadlineDate which is not in expected format defined in dateStringParser (or deadlineDate is nil as well).

Upvotes: 1

Related Questions