I have coded a timer using an NSTimer that updates a label. The problem is that in the same viewcontroller I have a uitableview and when I scroll it down, the timer doesn't update its value so the user can "cheat" to stop the timer.
I think this could be easy to fix with a serial queue with CGD but I don't figure out how to do it.
Thanks in advance,
Juan
First of all keep in mind that you cannot perform UI changes in any other thread than the main thread. Having said that, you need the
NSTimer
to fire in the main queue, otherwise the program will crash when changing theUILabel
. Have a look at this links http://bynomial.com/blog/?p=67 and this one http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.htmlTo my knowledge, if you schedule the timer in the for the
NSRunLoopCommonModes
it will ignore event updates and fire the timer jsut how you want it:I have faced same problem if u run timer in main thread than when you scroll tableview its stop timer too. Solution is that you run timer in background and update GUI in main thread
To update the UI you need to use
OperationQueue.main
. Supposing you have aUILabel
named timeLabel://--------- view did load