Better Programming

Advice for programmers.

Follow publication

Member-only story

Implementing SwiftUI TimelineView in iOS 15

Let’s build a digital clock using the new Canvas View

Mark Lucking
Better Programming
Published in
4 min readSep 27, 2021
A clock using the new SwiftUI 3.0 TimelineView

Have you heard of crontab? No, he isn’t a DC hero. It has been around since 1975, an eternity. iOS used to run a crontab, although looking through SO posts, it has long since disappeared. Of course, it is still around on OS X, type “crontab -l” under the shell and you’ll get a response.

But maybe all is not lost, since with iOS 15 Apple has launched a new view dedicated to running jobs on a schedule, a bit like crontab. I say run a job, I mean refreshing a view on a schedule. Now; I should confess at this point that I wasn’t quite sure how I would be supposed to use this new view. I wasn’t sure because we already had a scheduler in SwiftUI. A scheduler that looks like this.

let timer = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect()

A scheduler you could catch in your code and force a view refresh of sorts. A refresh of every 0.5 seconds in the case shown.

.onReceive(timer) { _ in
refresh += 1
}.id(refresh)

I can only assume they decided to create a view dedicated view to scheduling because it fits with the single source of truth philosophy in SwiftUI. A single source for scheduling all the code your app should be referencing. Join me on…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Mark Lucking
Mark Lucking

Written by Mark Lucking

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Responses (1)

Write a response