Migrating to Swift 3 Dispatch


(Thierry Passeron) #1

Hello All,

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:
???

Xcode is not helping me with any of the required refactoring. The convert tool throws many errors but no proposition.
I have tried to explore the New Dispatch headers with no luck. DISPATCH_SOURCE_TYPE_TIMER can’t be found… ok so It seems to be replaced with a protocol DispatchSourceTimer and … no initializer… I think I don’t yet catch with the whole philosophy of this new version.

I’d be pleased if anyone cared to enlighten me.

Thanks and regards,
Thierry


(Hooman Mehr) #2

Hello All,

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:

let timer = DispatchSource.makeTimerSource(queue: queue)
timer.scheduleRepeating(wallDeadline: .now(), interval: .seconds(1))
timer.setEventHandler {
    // Do something useful
}
timer.resume()

Isn’t it nice? It is a pity it is not well documented yet.

···

On Oct 16, 2016, at 11:25 AM, Thierry Passeron via swift-users <swift-users@swift.org> wrote:

Xcode is not helping me with any of the required refactoring. The convert tool throws many errors but no proposition.
I have tried to explore the New Dispatch headers with no luck. DISPATCH_SOURCE_TYPE_TIMER can’t be found… ok so It seems to be replaced with a protocol DispatchSourceTimer and … no initializer… I think I don’t yet catch with the whole philosophy of this new version.

I’d be pleased if anyone cared to enlighten me.

Thanks and regards,
Thierry

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #3

I’am not familiar with “timer dispatch source”. Do you want to run some closure at time X? Does this help?

            let dpt = DispatchTime.now() + delta * Double(NSEC_PER_SEC)
            queue.asyncAfter(deadline: dpt) { … }

Regards,
Rien.

···

On 16 Oct 2016, at 20:25, Thierry Passeron via swift-users <swift-users@swift.org> wrote:

Hello All,

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:
???

Xcode is not helping me with any of the required refactoring. The convert tool throws many errors but no proposition.
I have tried to explore the New Dispatch headers with no luck. DISPATCH_SOURCE_TYPE_TIMER can’t be found… ok so It seems to be replaced with a protocol DispatchSourceTimer and … no initializer… I think I don’t yet catch with the whole philosophy of this new version.

I’d be pleased if anyone cared to enlighten me.

Thanks and regards,
Thierry

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Ole Begemann) #4

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()


(Thierry Passeron) #5

Thank you all (Ole, Rien, Adrian)! It’s alive!

You are right it’s a shame how poorly it is documented at the moment. There are many overloads of « scheduleRepeating() » with different parameters and the headers are not that helpful…

Thanks again.

···

Le 17 oct. 2016 à 00:32, Hooman Mehr <hooman@mac.com> a écrit :

On Oct 16, 2016, at 11:25 AM, Thierry Passeron via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello All,

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:

let timer = DispatchSource.makeTimerSource(queue: queue)
timer.scheduleRepeating(wallDeadline: .now(), interval: .seconds(1))
timer.setEventHandler {
    // Do something useful
}
timer.resume()

Isn’t it nice? It is a pity it is not well documented yet.

Xcode is not helping me with any of the required refactoring. The convert tool throws many errors but no proposition.
I have tried to explore the New Dispatch headers with no luck. DISPATCH_SOURCE_TYPE_TIMER can’t be found… ok so It seems to be replaced with a protocol DispatchSourceTimer and … no initializer… I think I don’t yet catch with the whole philosophy of this new version.

I’d be pleased if anyone cared to enlighten me.

Thanks and regards,
Thierry

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users