Sunday, August 4, 2024
HomeiOS Developmentswift - Add timer to RunLoop that is already operating in iOS

swift – Add timer to RunLoop that is already operating in iOS


I’ve this code:


    let timer1 = Timer(timeInterval: 1, repeats: true) { _ in
      print("FOO1")
    }
    let timer2 = Timer(timeInterval: 1, repeats: true) { _ in
      print("FOO2")
    }
    let timer3 = Timer(timeInterval: 1, repeats: true) { _ in
      print("FOO3")
    }
    
    let queue = DispatchQueue(label: "")
    queue.async {
      RunLoop.present.add(timer1, forMode: .widespread)
      RunLoop.present.run()
    }
    queue.async {
      RunLoop.present.add(timer2, forMode: .widespread)
    }
    queue.async {
      RunLoop.present.add(timer3, forMode: .widespread)
    }

Within the present code, solely timer1 is run. That is unusual, as a result of timer2 and timer3 are added to a operating runloop. I recall in the principle run loop (which is already operating so I haven’t got to name run once more), I can merely add timers I need to add, and the timers will get triggered no downside.

My findings:

  • If I transfer RunLoop.present.run() to the final async block, then all timers can be run.
  • If I add RunLoop.present.run() to all the three async blocks, solely timer1 is run.
  • Altering .widespread to .default mode will not assist

I’m questioning why is my runloop behaving otherwise than the principle runloop, and the way am i able to mimic the conduct of predominant runloop?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments