Clocks: Existential Time

Episode #209 • Oct 17, 2022 • Subscriber-Only

The Clock protocol is a brand-new feature of Swift 5.7 for dealing with time-based asynchrony. We will explore its interface, compare it to Combine’s Scheduler profile, and see what it takes to write and use our own conformances.

Existential Time
Introduction
00:05
The Clock protocol
01:50
Injecting clocks
15:35
Immediate clocks
34:53
Unimplemented clocks
41:57
Next time: Test clocks
51:18

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Introduction

Swift 5.7 was released a few months ago, and with it came a new tool for dealing with time-based asynchrony, but it has mostly flown under the radar so far. I’m talking about the Clock protocol, which encapsulates the idea of suspending for an amount of time in an asynchronous context.

This concept is similar to schedulers that are native to the Combine framework. Schedulers encapsulates the idea of how to schedule work at a future date, or even repeatedly on an interval. Schedulers pair well with reactive programming frameworks such as Combine because they can not only express time-based asynchrony, but can also describe the execution context of where to perform work or deliver values, such as on a particular dispatch queue, operation queue or run loop.

But clocks operate in Swift’s new structured concurrency model, and so they can be a lot simpler than schedulers. They don’t need to describe how or where they are going to perform work. They can simply use a surrounding asynchronous context to suspend for an amount of time, and then once that time passes it will allow execution to continue flowing in its task. No need to think about threads or queues.

So, it’s great that clocks are simpler than schedulers, and of course it would be great if we could slowly replace any usages of schedulers with clocks in order to remove yet another dependency on the Combine framework. But, if we’re not careful, we will accidentally introduce time-based asynchrony into our features that is completely uncontrollable, making it harder to iterate on our features and harder, if not impossible, to test our features.

So, this episode we are going to explore the new Clock protocol to see why it’s so special, and then see what needs to be done so that we can take control of time rather than having it control us.

This episode is for subscribers only.

Subscribe to Point-Free

Access this episode, plus all past and future episodes when you become a subscriber.

See plans and pricing

Already a subscriber? Log in

References

SE-0329: Clock, Instant, and Duration

Philippe Hausler • Wednesday Sep 29, 2021

The proposal that introduced the Clock protocol to the Swift standard library.

Reply to Pitch: 'Primary Associated Types in the Standard Library'

Brandon Williams & Stephen Celis • Wednesday Apr 6, 2022

Originally there was some question as to whether or not the Clock protocol should have a primary associated type. We took to the forums to help motivate it.

SE-0374: Add `sleep(for:)` to `Clock`

Brandon Williams & Stephen Celis • Monday Sep 19, 2022

A Swift Evolution proposal from yours truly that introduced a sleep(for:) method to Clock, making it possible for clock existentials to sleep.

Downloads

Sample Code

0209-clocks-pt1