Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pranavss11

macrumors 6502
Original poster
Dec 29, 2007
360
0
San Jose
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:mad: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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.