In #SwiftUI I want to make something change every X seconds so I can use a timer like this example:
This seems to work but won't the timer get reset/recreated every time this view struct gets recreated by SwiftUI? Shouldn't the timer be a StateObject?
@bigzaphod yeah, def don’t store mutable class references directly on the view struct like that. You might be able to get away with a plain for the timer instead, since it’s not directly observable anyways, but in general factoring everything in a “view model” ObservableObject would be cleaner for something more complex.
@axiixc using State was my instinct here so I'm surprised to see something from Paul Hudson about SwiftUI that seems to be wrong - but maybe it is.
I’m thinking in c# And need to learn swift 😂
But in c# the timer isn’t something visual… I just put it higher in the hierarchy and have a visual updated only when showing. Hence confusion 😵💫
The original server operated by the Mastodon gGmbH non-profit