views:

49

answers:

1

Hello everyone.

My question is quiet simple (I think). I have a class that implements a simple chronometer (using some integers and NSTimer). I would like to close my app (so enter in background mode) but I would like my chronometer still continue to count. How can I manage that ?

Thanks a lot !

A: 

Save the time which was present when you started the timer. Something like self.timerStartDate = [NSDate date];. Calling NSTimer once a second and increment a integer will give you very inaccurate results.
From the NSTimer Class Reference :

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

If you use the approach with saving the startTime your Timer even "runs" if the program is not running


EDIT: yes. it is wrong to use a timer to count time.
I just wrote a little test case. An app with a simple tableview with 12 rows. Two timers, one fired every millisecond, the other is fired every 10 seconds.
In the first timer I add 1 to an Integer, in the second one I print the result of the first timer and the time which passed since the last time I printed, measured with mach_absolute_time().
Look at the differences, the first measurements are quite ok, but if I start scrolling in that simple UITableView, which runs in the simulator on my powerful mac you get a big difference.

2010-10-27 11:16:49.266 MutliTableTest[24726:207] Calculated: 9.988000000000
2010-10-27 11:16:49.267 MutliTableTest[24726:207] Measured  : 10.000055888000
2010-10-27 11:16:59.266 MutliTableTest[24726:207] Calculated: 9.986000000000
2010-10-27 11:16:59.267 MutliTableTest[24726:207] Measured  : 9.999898500000
2010-10-27 11:17:09.608 MutliTableTest[24726:207] Calculated: 8.091000000000
2010-10-27 11:17:09.609 MutliTableTest[24726:207] Measured  : 10.341779530000
2010-10-27 11:17:19.266 MutliTableTest[24726:207] Calculated: 1.966000000000
2010-10-27 11:17:19.267 MutliTableTest[24726:207] Measured  : 9.658319274000
2010-10-27 11:17:29.266 MutliTableTest[24726:207] Calculated: 9.991000000000
2010-10-27 11:17:29.267 MutliTableTest[24726:207] Measured  : 9.999891531000

So don't use NSTimer, use a NSDate and get the difference between current time and the startTime with something like NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:self.startDate];

But I wonder why you need a differenct timeStamp every millisecond

fluchtpunkt
So for you it's not a good idea to use incrementation of integers (milliS, second, minute,etc ...). So it's better to use a starting date and then every millisecond (with a NSTimer) get the current date and make a subtraction. Is that right ?
Pierre
It's a chronometer so I want the same system as my digital watch for example. And I made NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:self.startDate] but how can I refresh it every millisecond without timer ?
Pierre
Every millisecond is 1000 times in a second, which is 1000 Frames per second. I doubt this is possible, but you could use a timer for this, that's what timers are used for. Just don't do any real-time things with Timers. And 30 redraws a second should be more than enough.
fluchtpunkt
it's crashing when i use a timer ... are you sure this is a good idea to use time interval for this sort of application ?
Pierre
Don't forget to retain the NSDate which is your startDate... Using timeIntervals is the only approach you can use if you want accurate time measurement. You could use mach_absolute_time() to measure time, but if it has to be this accurate I would not use an iPhone anyway. Have you ever measured the accuracy of your approach? Like let the program run 30 minutes and use a real stopwatch as comparison? I doubt it. Btw, calling a timer 1000 times a second brings the cpu-load on my ipad to almost 100%.
fluchtpunkt
I understand you but how to measure time interval "every time". I have an headache now :/
Pierre