I'm new to SwiftUI and MVVM, and the whole though process around @Published is still not making total sense to me.
In my contentView, I have an observable object that reads from a class that adopts the ObservedObject protocol.
Content View:
struct ContentView: View {
@ObservedObject var exercises: ExerciseSelector = ExerciseSelector()
Separate Class:
class ExerciseSelector: ObservableObject {
@Published var activeExercises : [SelectedExercises] = []
On another page in my app (a different tab in a TabView), I have a button that should change the 'activeExercises' array.
struct NewRoutine: View {
let exerciseSelector = ExerciseSelector()
...
//Button Function
func printThings() {
exerciseSelector.select(random: true)
I've put print statements in there to confirm that it did, in fact, change the array (and it does), but it still doesn't show on the first page. My guess is that I'm messing something up by initializing another instance of ExerciseSelector on the second page, and that it's technically not the same one as the ContentView is observing. So I tried to fix this by creating a singleton within the ExerciseSelector class that both views can read off of.
static let shared = ExerciseSelector()
This works, but I feel like it's not really the "correct" way to do it.
Is there a better way to make this all work, or is the singleton approach the best way to do it?
@EnvironmentObject
strategy is the one that more closely resembles the way SwiftUI is supposed to be used. - West1@EnvironmentObjects
is the injection. You end up with a two step process to use it, and I have found it be a little flaky at times. I will admit not having used it since 5.3. I agree that in thought@EnvironmentObject
is the preferred method, but@ObservableObject
ticks all the same boxes. - Yrb