I have created an ObservableObject in a View.
@ObservedObject var selectionModel = FilterSelectionModel()
I put a breakpoint inside the FilterSelectionModel
's init
function and it is called multiple times. Because this View is part of a NavigationLink
, I understand that it gets created then and along with it, the selectionModel. When I navigate to the View, the selectionModel is created again.
In this same View I have a "sub View" where I pass the selectionModel as an EnvironmentObject
so the sub-view can change it.
AddFilterScreen().environmentObject(self.selectionModel)
When the sub view is dismissed, the selectionModel is once more created and the changes made to it have disappeared.
Interesting Note: At the very top level is a NavigationView
. IF I add
.navigationViewStyle(StackNavigationViewStyle())
to this NavigationView
, my selectionModel's changes disappear. BUT if I do not add the navigationStyle
, the selectionModel's changes made in the sub view remain!! (But I don't want a split nav view, I want a stacked nav view)
In both cases - with or without the navigationStyle
, the selectionModel is created multiple times. I can't wrap my head around how any of this is supposed to work reliably.