0

This is slowly beginning to drive me insane because it doesn't make sense... I'm using Swift and can't seem to get this timer to start no matter what I do.

I have an NSObject that's handling all my time-related things in my app, and in that NSObject I have a function that initializes a timer. It looks like this:

 class Time: NSObject {

  static let sharedInstance = Time()
  private override init(){}

         var roundTimer = Timer()


         //This is called from my splash screen while the app is loading
     func initializeTimers(){

            //Initialize the round countdown timer

  roundTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(updateRoundTimerCountdown)), userInfo: nil, repeats: true)
    print("The round timer should have started")

            }

    @objc func updateRoundTimerCountdown(){print("Round Timer ran"); Time.roundTimerIsAt -= 1; print("Round Timer: \(Time.roundTimerIsAt)")}

        }

From my splash screen where the app loads all the user data, I call Time.sharedInstance.initializeTimers() in order to start it. I get to the point of it printing "The round timer should have started" to the debugger (which happens after the line that should start the timer) but the selector isn't hit at all. The "Round Timer ran" isn't printed to the debugger nor does the timer appear to have started, so what am I doing wrong?

I appreciate any help, even if the answer is glaringly obvious lol :P I've spent too much time on this!

Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
Justin
  • 319
  • 3
  • 13
  • I ran your code and it works for me, what is your "roundTimerIsAt" variable? – NicolasElPapu Nov 02 '19 at 21:44
  • @NicolasElPapu In my app, a 'round' runs for a complete week so when I launch my app I first gather information from my server regarding where we are in the current week. It does some calculations and sets "roundTimerIsAt" to be the number of seconds that are remaining in this week's round. The timer is created to incrementally decrease this value by 1 every second in order to display a live countdown to the user. It wasn't really important so I left it out but forgot to remove it from the selector function... I wonder why it's working for you? :/ – Justin Nov 02 '19 at 21:57
  • Mmmmm but why do you call it as Time.roundTimerIsAt if its in the class? Im going to post the code that worked for me maybe it can help you – NicolasElPapu Nov 02 '19 at 22:07

1 Answers1

0

I actually finally got it working! I'm sorry...there would have been too much code if I posted my entire splash screen view controller and Time NSData, but it appears the issue was elsewhere. But maybe someone else may run into this so this may help.

On my splash screen, I perform 15 'app-loading steps' that include several steps that gather information from servers and such... those steps I progress through by using completion handlers to ensure that the data is actually collected.

It's after collecting some of this data that I call the Time.sharedInstance.initializeTimers() function. My call to the servers runs on a different thread other than the main, so apparently by calling this function after the completion handler runs, it's still on the other thread and these timers can't start on anything other than the main thread!?

So all I did was this:

DispatchQueue.main.async {Time.sharedInstance.initializeTimers()}

And it works now... (facepalm). Doesn't make a whole lot of sense to me but... it works :P

Justin
  • 319
  • 3
  • 13