Dark mode switching modifies ObservedObject

This is a SwiftUI Mac App.

There is an observable object with a published array

class AllObjects : ObservableObject {
    @Published var theItems = [Item]()
}

In the View, observed with:

    @ObservedObject var allObjects : AllObjects 

Item class is :

class Item: ObservableObject, Identifiable {
    let id = UUID()
    var placeNo = 1 
    // Other properties
}

When I switch dark / light mode within the app (with a toggle)

    @Environment(\.colorScheme) var colorScheme 

the ObservedObject allObjects is modified: array is emptied: allObjects.theItems is now an empty array.

What could cause the change to theItems ? I have checked that the only func un which theItems is reset to [] is not called.

Answered by in
Accepted Answer

Who owns the AllObjects? How do you instance the view? Does it happen if you use @StateObject instead of @ObservedObject?

Let me know what happens.

Thanks, that's it. I changed to StateObject and it works.

Dark mode switching modifies ObservedObject
 
 
Q