Here’s my code:
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString *currentTime = [dateFormatter stringFromDate:currDate];
NSDate *now = [NSDate date];
int interval = 1*60; // one minute (minutes*60)
long int nowSeconds = (long int) [now timeIntervalSince1970];
int secondsLeft = interval - (nowSeconds % interval);
NSDate *nextIntervalDate = [NSDate dateWithTimeIntervalSince1970:nowSeconds+secondsLeft];
NSString *nextTrigger = [dateFormatter stringFromDate:nextIntervalDate];
timeRemaining.text = [NSString stringWithFormat:@"%02d:%02d", secondsLeft/60, secondsLeft%60];
NSLog([NSString stringWithFormat:@"%@:%@", currentTime, nextTrigger]);
if ([currentTime isEqualToString:nextTrigger]) {
}
Problem is, ‘if ([currentTime isEqualToString:nextTrigger])’ is never equal. Because nextTrigger changes to the next time segment before they’re equal. Here is the NSLog:
2013-01-17 15:54:59.987 app[35987:c07] 15:54:59:15:55:00
2013-01-17 15:54:59.997 app[35987:c07] 15:54:59:15:55:00
2013-01-17 15:55:00.007 app[35987:c07] 15:55:00:15:56:00 <----RIGHT HERE
2013-01-17 15:55:00.016 app[35987:c07] 15:55:00:15:56:00
My code’s nextTrigger is basically a round up of the current time by the nearest minute. ALSO, this whole code segment is in a repeating NSTimer every .1 seconds.
How can I fix this?
I had a somewhat similar problem. What I did was make a copy and check it by that.(one thing is to have both
oldTriggerandnewTriggerdeclared already)Check this code