NSTimer does not fire in next date time
I need to fire timer on the basis of the time selected in the date picker. Now if the selected time is greater than my current time then the NSTimeInterval returns positive value and the timer fired correctly.
My problem is when I select the time which is less than the current time then the NSTimeInterval returns negative value.
So Now what I does is :- add seconds so that it gets fired next day by 24*60*60.
The seconds value is coming correct because when I test this by changing the time into date it returns the next date with the selected time.
I use these seconds value in setting my timer.
Here is my code :
NSTimeInterval value=[[self.pickerView date] timeIntervalSinceDate:[NSDate date]]; value=value+24*60*60; NSLog(@"value =%f",value); NSDate *d1 = [NSDate dateWithTimeIntervalSinceNow:value]; NSLog(@"%@",d1); NSDateFormatter *format=[[NSDateFormatter alloc]init]; [format setDateFormat:@"dd/MM/yyyy hh:mm:ss a"]; NSString *str=[format stringFromDate:d1]; NSLog(@"str=%@",str); [format release]; NSTimer *t = [[NSTimer alloc] initWithFireDate: d1 interval: 1 target: app selector:@selector(fireWakeUpTimerMethod:) userInfo:dict repeats:NO]; // app.WakeUPTimer=t; NSRunLoop *runner = [NSRunLoop currentRunLoop]; [runner addTimer:t forMode: NSRunLoopCommonModes]; [t release];
But the timer is not getting fired.
I tested it like this:- Suppose my device current time is 15 june 11:00 am then I select the 10:00 am time in picker view , since 10:00 am is less than 11:00 am then the time interval returns negative value so in this case I do value=value+(24*60*60); and set my timer with this value .Then I open the settings app and change my device date and time to 16 june 9:58 am and close settings app and open my app.But the method did not fire at 10:00 am.Please tell us if this is the correct way of testing this scenario.or I am doing some mistake in testing instead of coding.
Please help me as I am stuck here.
Thanks in advance!
I assume you are getting value correct:
Also check target of NSTimer and its selector