I have a simulation model that should update itself variably once every 0.5 - 5 seconds (roughly) in the background with the frequency a function of the model's state.
I am using a SwiftUI app that displays some of the data from this simulation model, and the SwiftUI interface already pulls the published data from the model just fine. I currently have some external controls (e.g. a slider, a toggle button, etc.) that I can use to manipulate the state of the model and this is reflected in the SwiftUI views that display the data. What I want to do now is make the model update itself on a timer (again, it's a simulator) and when the published data changes, SwiftUI already handles having the views re-draw themselves with the updated data.
So what I'd like to do:
class MyModel: ObservableObject {
static let shared shared = MyModel()
@Published var coolData1: CoolData()
@Published var coolData2: CoolData()
@Published var coolData3: CoolData()
var modelTimer = AnyPublisher<Date,Never>
func runModel() {
modelTimer = Timer.publish(every: 1, on: .main, in: .common)
.autoconnect()
.eraseToAnyPublisher()
// HERE I want updateModel() called every time the modelTimer fires and it should run in the background.
}
func pauseModel() {
modelTimer.upstream.connect().cancel()
}
func updateModel() {
update(coolData1)
update(coolData2)
update(coolData3)
}
}
I just don't know where or the specific syntax with Combine/Timer to get the model to update on my dynamic schedule so it runs in the background.
Thank you for your help.