I am writing an app that pulls data from a website and displays it using SwiftUI.
My main view has an @ObservedObject
which is of type DataStore()
, and which is used in the view:
struct ContentView: View {
@ObservedObject var store = DataStore()
var body: some View {
List(store.zones) { zone in
...
The data model is defined as:
class DataStore: ObservableObject {
@Published var zones: [SonosZone] = []
init() {
getZones()
}
func getZones() {
Api().getZones { (zones) in
self.zones = zones
}
}
}
I want to call getZones()
every 30 seconds. How do I do this?
I'm guessing that I use code like this somewhere, but I don't know where to put it, or how to call the function in the closure:
_ = Timer.scheduledTimer(withTimeInterval: 30, repeats: true) { _ in
getZones()
}