List not refreshing with EnvironmentObject

It's my understanding that when a view contains an EnvironmentObject and the object changes in any way, the view should refresh. Is that right?


I've got the following view:


struct ContentView: View {

@EnvironmentObject var globalInfo: appInfo

var body: some View {


NavigationView {

List {

ForEach(globalInfo.users.indices, id: \.self) { idx in

NavigationLink(destination: UserView(idx: idx)) {

Text(self.globalInfo.users[idx])

}

}

}

.navigationBarItems(leading: NavigationLink(destination: AllUserView()) { Text("Show") },

trailing: NavigationLink(destination: NewUserView()) { Text("Add") })


}

}

}


UserView and NewUserView include the same EnvironmentObject declaration and everything seems to be working fine. With breakpoints or using the AllUserView, I can see that changes or additions I make are getting correctly reflected in appInfo, but the list of names in this view never get updated. I've stripped everything out of my app down to this minimal implementation to try to understand the issue but I'm obviously missing something.

I’m seeing something similar in an app I’m working on, but haven’t had the time to truly isolate it as you have. In my guess, the data clearly exists as the detail view (an action sheet) show it’s fine, but the list view doesn’t show any data.

My guess it's because ForEach. Try to add some dumb view which will cause UI update outside of the loop. Something like this pseudo code;


NavigationView {
            Text(globalInfo.users.first? ?? "Default")
            List {
                ForEach(globalInfo.users.indices, id: \.self) { idx in
                    NavigationLink(destination: UserView(idx: idx)) {
                        Text(self.globalInfo.users[idx])
                    }
                }
            }


...this is pseudo code, wrap it also into some kind of Stack view

To follow up on this, I am seeing the same thing, and I think it has to do with the NavigationLink destination not being updated
I'm having the same issue. The data in the array in the EnvironmentObject is correctly updated, but the forEach/View is not.
Have you tried to add @Published to the users.indices property in your appInfo class?
List not refreshing with EnvironmentObject
 
 
Q