-1

In my app I declare a timer:

entropy = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("tick"), userInfo: nil, repeats: true)

Where entropy is:

var entropy: NSTimer

Then, within ApplicationWillResignActive entropy is invalidated:

main.entropy.invalidate()

Where main is:

let main = ViewController()

But then, when the application becomes active, the timer resumes from where it last was when the app resigned active.

What am I missing here?

Regards, Brandon

Brandon Bradley
  • 3,160
  • 4
  • 22
  • 39
  • Where are you initialising the timer. If the initialisation of timer is more than once, even though if you have only one instance, there will be temporary timers created which might cause the problems. – Varun Jan 17 '16 at 17:54
  • If the current timer is invalidated before a new one is initialized, does that count as multiple initializations? – Brandon Bradley Jan 17 '16 at 18:17
  • No but if entropy is initialised again and again before invalidating, then that'll create multiple instances. So where did you initialise the entropy? – Varun Jan 17 '16 at 19:02
  • Then no, the only initialization is this: ` if (commenceButton.currentTitle == "Start") { commenceButton.enabled = false commenceButton.setTitle("Stop", forState: .Normal) entropy = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("tick"), userInfo: nil, repeats: true) commenceButton.enabled = true } else { commenceButton.enabled = false entropy.invalidate() commenceButton.setTitle("Start", forState: .Normal) commenceButton.enabled = true }` – Brandon Bradley Jan 17 '16 at 19:05

1 Answers1

0

This was the solution:

https://stackoverflow.com/a/27700746/4418557

Basically, I needed to add observers to my ViewController. For more details click the link above.

Community
  • 1
  • 1
Brandon Bradley
  • 3,160
  • 4
  • 22
  • 39