2

I'm working on a multiview app. One of the views is a table view. Each cell has a stopwatch. I'm planning to use NSTimer for the stopwatches. Do I need to implement multithreading for the timers to work properly even when the user switches the view and then comes back later? I did my research but most of the tutorials cover one NSTimer in a single view. I want to make sure the user can do other things while the timers are running, like use the interface, navigation, etc. In another post Placing an NSTimer in a separate thread someone said you need a different runloop for the timer. Would I need one runloop for each timer in my case? Is it advisable? Any performance drawbacks?

Thanks a lot!

Community
  • 1
  • 1

1 Answers1

2

One run loop should be just fine. Your interface will still be responsive.

Keep in mind that timers are never guaranteed to be accurate. They are affected by how much other stuff is on the same loop. Its ok to use the timer to update the display but not to actually measure time. Set an NSDate when you start a stop watch then compare the current date with that start date each time your display timer updates the display.

Since you should only use the NSTimer to update the display, could you just use one generic display update timer that updates all running stopwatches, instead of having one for each stopwatch?

Dancreek
  • 9,524
  • 1
  • 31
  • 34
  • Thanks for your answer @Dancreek ! I will be working on implementing this tonight and over the next days. I will keep you posted. I like the idea of 1 timer updating all stopwatches. Every time the Timer fires I can loop through all stopwatches and if their status == running I can update their display. It's a great suggestion actually. However Im confused about the accuracy. You said dont use it to track time. But a stopwatch does exactly that. When the timer fires i should increment a stopwatch by 1 second. How can I go about this ? Thanks – Alexandru Lucian Susma Mar 21 '12 at 19:45
  • I just reazlied what you meant by the NSDate and that's exactly what I have already implemented in my app. I have an occurrence object for each stopwatch and it has a START_TIME as attribute. Using current time and START_TIME I can easily computer the TIMEINTERVAL ! thanks !! – Alexandru Lucian Susma Mar 21 '12 at 19:53