PDA

View Full Version : NSTimer non repeating timer - retrieve timeInterval.




pranavss11
Jul 30, 2010, 11:21 AM
I have posted this question on stackoverflow but have not gotten any responses. Maybe someone here will be more helpful ? :D


http://stackoverflow.com/questions/3373136/nstimer-non-repeating-timer-retrieve-timeinterval


Hi guys,

I am using a NSTimer instance that is called after 300 seconds. The code is something like this:

timer = [[NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(aMethod) userInfo:nil repeats:NO] retain];

I understand that since this is a non-repeating timer, that [timer timeInterval]; returns 0. Is there a way that I can get the elapsed time? I need to use the time that is elapsed (say 20 seconds) as when the application enters background on iOS4, the timer gets suspended and I need to get the elapsed time for the timer so I can subtract this elapsedTime + time the app was in background from the original 300 and do something if the time is exceeded.



chown33
Jul 30, 2010, 12:10 PM
Seems obvious to me, but why not compare fireDate to now? Surely this is a simpler calculation than summing the timer's remaining time, the time spent suspended, etc. and comparing to the original 300.

If fireDate won't work for you, then put the interval in the userInfo dictionary.