FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

How to make a task sleep

Paul Hudson    @twostraws   

Swift’s Task struct has a static sleep() method that will cause the current task to be suspended for at least some number of nanoseconds. Yes, nanoseconds: you need to write 1_000_000_000 to get 1 second. As this will cause the task to be suspended, you need to call Task.sleep() using await.

For example, this will make the current task sleep for at least 3 seconds:

Task.sleep(3_000_000_000)

Important: Calling Task.sleep() will make the current task sleep for at least the amount of time you ask, not exactly the time you ask. There is a little drift involved because the system might be busy doing other work when the sleep ends, but you are at least guaranteed it won’t end before your time has elapsed.

Using nanoseconds is a bit clumsy, but Swift doesn’t have an alternative at this time – the plan seems to be to wait for a more thorough review of managing time in the language before committing to specific API.

In the meantime, we can add small Task extensions to make sleeping easier to accomplish. For example, this lets us sleep using seconds as a floating-point number:

extension Task {
    static func sleep(seconds: Double) async {
        let duration = UInt64(seconds * 1_000_000_000)
        await sleep(duration)
    }
}

With that in place, you can now write Task.sleep(seconds: 0.5) or similar.

One thing to be careful of is that Task.sleep() does not check for cancellation, which means two things:

  1. If you call Task.sleep() on a cancelled task, it will still sleep for your requested duration.
  2. If you cancel a sleeping task it won’t be woken prematurely.

We can fix up the first of those by adding a couple of helper methods in a Task extension:

extension Task {
    static func sleepUnlessCancelled(_ duration: UInt64) async {
        if Task.isCancelled == false {
            await Task.sleep(duration)
        }
    }

    static func sleepUnlessCancelled(seconds: Double) async {
        if Task.isCancelled == false {
            let duration = UInt64(seconds * 1_000_000_000)
            await Task.sleep(duration)
        }
    }
}

With those in place you can at least stop new sleeps from happening when a task is cancelled.

As for the second problem, this is trickier because Task.sleep() is designed to stay asleep even when the task is cancelled. One simple workaround is to make a snooze() extension that slices a sleep up into smaller amounts then sleeps repeatedly while checking for cancellation:

extension Task {
    static func snooze(_ duration: UInt64) async {
        let snoozeSlice = duration / 10

        for _ in stride(from: 0, through: duration, by: Int(snoozeSlice)) {
            if Task.isCancelled == false {
                await Task.sleep(snoozeSlice)
            }
        }
    }

    static func snooze(seconds: Double) async {
        let duration = UInt64(seconds * 1_000_000_000)
        await Task.snooze(duration)
    }
}

The problem with this approach is that it’s going to exacerbate the drift – calling Task.sleep(4_000_000_000) might sleep for 4.2 seconds in total, but calling Task.snooze(4_000_000_000) might sleep for 4.7 seconds in total.

A better option might be to use something like clock_gettime_nsec_np() to read the current system clock, then repeatedly sleep small amounts of time until the target time has passed. Here’s how that looks:

extension Task {
    static func snooze(seconds: Double) async {
        let duration = UInt64(seconds * 1_000_000_000)
        let target = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) + duration

        repeat {
            await Task.sleep(100_000_000)
        } while clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) < target
    }
}

That will keep the sleep drift low, while also allowing our task to be cancelled while snoozing. However, sleeping then checking for cancellation 10 times a second does add extra work to the system so I’d be careful before doing so – or at least do a fair amount of extra research before relying on it!

Tip: Unlike making a thread sleep, Task.sleep() does not block the underlying thread, allowing it pick up work from elsewhere if needed.

Hacking with Swift is sponsored by Essential Developer

SPONSORED From August 2nd to 8th you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Save your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.