Updating SwiftUI view for every x seconds

Photo by Markus Winkler on Unsplash

With the evolution of SwiftUI, even smaller things in iOS Development became worth re-visiting.

In this article we will update a SwiftUI View for every 1 second using few possible ways.

Just created a View Model with timer and small refresh fresh function which increments index and update date. And now we can use the View Model inside SwiftUI View.

With Just StateObject instance the view refreshes every second. The @State instance is just to show that we can still maintain state of view.

Starting with iOS15, we can use new TimelineView to update view periodically. We get timeline instance having date at which update triggered and rate at which update happening. This helps in refreshing the view for every second. This view shows output as follows:

Apart from achieving the 1 second refresh, the “lower bound must be less than upper bound” condition might be reason behind delay.

Here again we just used Combine to publish update for every second.

That’s it !!!

Thanks for Reading…



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sai Durga Mahesh

Using Data Science to provide better solutions to real word problems