[swift-users] Migrating to Swift 3 Dispatch

Ole Begemann ole at oleb.net
Sun Oct 16 14:20:36 CDT 2016


> I’m in the process of migrating older code to Swift 3 and I’m stuck on this one.
> How do you create a timer dispatch source?
>
> Old code:
>
> let source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue)
>
> dispatch_source_set_timer(source, dispatch_time(DISPATCH_TIME_NOW, 0), UInt64(interval * 1000_000_000), 0)
> dispatch_source_set_event_handler(source) {
>    // Do something useful
>  }
>
>  dispatch_resume(source)
>
> New code:

It should be something like this (untested):

     let timer = DispatchSource.makeTimerSource(flags: ..., queue: queue)
     timer.scheduleRepeating(deadline: ..., interval: ..., leeway: ...)
     timer.setEventHandler {
         ...
     }
     timer.resume()



More information about the swift-users mailing list