-1

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

}
KissTheCoder
  • 679
  • 6
  • 16
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • 1
    There is a lot of code missing to be able to reproduce this but how do you initialize the `@EnvironmentObject` in `detailHeaderCard`? The parent of an `@EnvironmentObject` should be an `@ObservableObject` or an `@StateObject`. Also, `trackerDataStore` is spelled exactly the same for the `var` and the `class` but differently in the type of the `var`. The Apple SwiftUI Tutorials are a good way to get familiar with the process https://developer.apple.com/tutorials/swiftui/handling-user-input – lorem ipsum Oct 26 '20 at 23:02

1 Answers1

0

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.

GarySabo
  • 5,806
  • 5
  • 49
  • 124