DispatchTime - Initializer

The new DispatchTime type appears to hold raw values related to mach_absolute_time() - how can I create a DispatchTime given an 'absolute time'?


The new DispatchWallTime does have a init() function based on a timespec; but, DispatchTime has no analogous function nor it is possible to convert from DispatchWallTime to DispatchTime.

I cannot find any initializer of DispatchTime, nor any way to convert DispatchWallTime to DispatchTime.


Use always DispatchWallTime, if you need absolute time.


By the way, do you know why those two distict types are provided in Swift Dispatch library? Both representing dispatch_time_t in GCD.

You manipulate times in ways like


let t = DispatchTime.now() + DispatchTimeInterval.milliseconds(30)

or

let wallt = DispatchWallTime.now() + DispatchTimeInterval.seconds(150)

I know that usage.


GoZoner is talking about how we can manage absolute time as DispatchTime (or DispatchWallTime), not a relative time from now.

By the way, do you know why those two distict types are provided in Swift Dispatch library?

These model the difference between wall time (returned by

gettimeofday
) and Mach absolute time (returned by
mach_absolute_time
). The latter is independent of system clock changes but stops when you sleep. In the C API everything is flattened to Mach absolute time, which means you lose critical info. Consider what happens if you schedule a timer for 1 second in the future and then the system sleeps for 2 seconds.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, eskimo! I really expect such description would be added to the Standard Linbrary refrences. (A problem of time?)

Can we expect the addition of an initliazer for DispatchTime in the next release?

The problem is that this doesn't seem to be true — at least not on iOS.


In my experiments, `asynchAfter(deadline:)` and `asynchAfter(wallDeadline:)` do exactly the same thing. If you start them at the same time with the same interval, they fire at the same time, no matter what happens in between.


Moreover, `asynchAfter(wallDeadline:)` does not successfully watch the clock. If `asynchAfter(deadline:)` would fire late because the app was suspended for a while, `asynchAfter(wallDeadline:)` fires equally late.


I'd love to be shown wrong. If there's a genuine difference — in fact, not just in theory — I'd like to know what it is.

The problem is that this doesn't seem to be true — at least not on iOS.

That wouldn’t necessarily surprise me but…

If

asynchAfter(deadline:)
would fire late because the app was suspended for a while …

App suspension is not the same as system sleep, and only the latter effects Mach absolute time.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Indeed. My experimentation suggests that DispatchWallTime on iOS is a synonym for DispatchTime. That's not evil, but it would be nice if the docs would say so.

My experimentation suggests that

DispatchWallTime
on iOS is a synonym for
DispatchTime
.

Thanks for digging into this.

That's not evil, but it would be nice if the docs would say so.

I believe you know what to do here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
DispatchTime - Initializer
 
 
Q