1

I got following code repeating a method every 1 second, and it works properly:

var timer : NSTimer?

override func viewDidLoad() {
  super.viewDidLoad()
  self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.timerFired(_:)), userInfo: nil, repeats: true)
}

func timerFired (timer: NSTimer) {
  print ("timer fired")
}

Issue

Above code works fine until the user start scrolling a UIScrollView in a sibling View Controller. Immediately upon scrolling 'timer fired' logs stops appearing. As soon as the user stops scrolling and lift the finger, 'timer fired' logs resume stamping every second.

Note: The UIScrollView and the sibling View Controller I mentioned has no association with this or another NSTimer. It doesn't have ANY reference to this class.

Things Tried:

I tried putting this line immediately after timer scheduling:

NSRunLoop.mainRunLoop().addTimer(self.timer!, forMode:NSDefaultRunLoopMode)

I also tried making sure all the timer activities are on main thread:

dispatch_async(dispatch_get_main_queue(),{
   self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.timerFired(_:)), userInfo: nil, repeats: true)
 })

I do have other threads running, if this seems to be thread related, yet I am really puzzled why this is happening EXACTLY when scrolling.

Gizmodo
  • 3,151
  • 7
  • 45
  • 92
  • NSRunLoop.mainRunLoop().addTimer(self.histogramUpdateTimer!, forMode:NSRunLoopCommonModes) fixed it. Emphasis on "NSRunLoopCommonModes" – Gizmodo Sep 19 '16 at 14:27

0 Answers0