Search code examples
iosswiftswiftuipublisherenvironmentobject

SwiftUI Environment Object Not Getting Updated


I thought it was the case that @Published properties inside a class designated as an environment object would automatically update to all the subviews I pass the environment object to, but the below code isn't updating, what am I doing wrong?

class trackerDataStore: ObservableObject {
    
    let healthStore = HKHealthStore()
    
    @Published var isLoadingWorkouts = false
    @Published var subscriptionIsActive = false
    @Published var trackerWorkoutObject: trackerWorkoutObject?
    
}
    

struct detailHeaderCard: View {
    
    @EnvironmentObject var trackerDataStore: TrackerDataStore
    
    var body: some View {
        //omitted
    }
    .sheet(isPresented: $isShowingPlayerAddStatsFormAsModal) {
        PlayerAddStatsForm(isShowingPlayerAddStatsFormAsModal: $isShowingPlayerAddStatsFormAsModal)
            .environmentObject(trackerDataStore)
    }
    
}
   
 
struct PlayerAddStatsForm: View {
    
    @EnvironmentObject var trackerDataStore: TrackerDataStore
    //not getting reactively updated here

}

Solution

  • I found out that the way I wired up @EnvironmentObject actually works well, I had a bug in my code in another area that was preventing the view from receiving the data I was looking for that made me think it was related to @EnvironmentObject not working properly.