I have a simple single-window application. It displays zero or more groups of controls in that main window based on "configurations" managed in a Settings (Preferences) view.
The "configuration" for a group is a structure (conforming to Identifiable, Codable) with an id, a string to hold a label for the group, and a handful of booleans that if true will show a particular control and not show it if false. An array of this structure is @Published
in a class conforming to ObservableObject
, and instances of the class are stored in UserDefaults. (It's code taken from one of Paul's examples.)
In the @main App
structure, I declare a @StateObject var configs = Configs()
. In the body
of this App
my Scene
has a Window
for the usual ContentView
and a Settings
for my PreferencesView
. All standard so far. Also standard: in both the PreferencesView
and the ContentView
I declare an @ObservedObject var configs : Configs
.
In the body
of ContentView
I check to see if the array that's in configs
is empty. (At first program launch it is.) If empty, a Text
is shown saying "please configure at least one thing." Otherwise, a ForEach() block iterates on the array and instantiates one or more ChannelView
s. A ChannelView
shows the controls enabled for the channel. A different Settings
structure type holds the values for each control.
The PreferencesView
has code to create, add and modify the configs
.
Here's the weird part. As I modify the configs
object (adding to or changing the contents of the array it contains), I expect that the ContentView
window should update and show newly-added Channels or disppear Channels that were deleted in the Preferences. That does not happen. However, if I close the application and re-start it, the main window shows the Channels that were configured previously.
I don't understand this behavior. So the question: how do I force the view in ContentView
to immediately update/refresh/reload/whatever when the configs
object updates?