0

I am trying to create a timer background task. I use DispatchSourceTimer and DispatchQueue with new methods Swift 3.

But it does not work...

var timer: DispatchSourceTimer!
let queue = DispatchQueue(label: "fr.yseemonnier.app.timer", attributes: [])

timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: UInt(0)), queue: queue)
timer.scheduleRepeating(deadline: .now(), interval: Double(5 * NSEC_PER_SEC), leeway: .seconds(1))
   timer.setEventHandler {
       DispatchQueue(label: "fr.yseemonnier.app.timer.async").async {
          print("Background task...") //refresh all 5 seconds...
       }
    }
timer.resume()

Thank you,

YM

YMonnier
  • 1,384
  • 2
  • 19
  • 30
  • 2
    "it does not work" means what? – Do you keep a reference to the timer? Otherwise it will be deallocated. Compare http://stackoverflow.com/questions/39580824/dispatchsourcetimer-and-swift-3-0 (duplicate?) – Martin R Sep 30 '16 at 08:58
  • I didn't see this topic. thanks... I will close this issue. – YMonnier Sep 30 '16 at 09:09
  • 2
    Your time interval is approx. 158 years ;-). Use simply `.seconds(5)` and you can omit the complete `flags` parameter. – vadian Sep 30 '16 at 09:12
  • Oh. I see, thx. :S – YMonnier Sep 30 '16 at 09:15

0 Answers0