I have such ObservableObject that I am injecting into views hierarchy by using environmentObject().
class MenuModel: ObservableObject {
@Published var selection: Int = 0
@Published var isMenuOpen: Bool = false
@Published var tabItems : [TabItem] = [TabItem]()
// {
// didSet {
// objectWillChange.send()
// }
// }
@Published var menuItems : [MenuItem] = [MenuItem]()
// {
// didSet {
// objectWillChange.send()
// }
// }
//var objectWillChange = PassthroughSubject<Void, Never>()
}
And here are issues I do not really understand well: 1. Above code with works correctly, as all properties are @Published. 2. But If I change it to something like this
class Click5MenuModel: ObservableObject {
@Published var selection: Int = 0
@Published var isMenuOpen: Bool = false
var tabItems : [TabItem] = [TabItem]()
{
didSet {
objectWillChange.send()
}
}
var menuItems : [MenuItem] = [MenuItem]()
{
didSet {
objectWillChange.send()
}
}
var objectWillChange = PassthroughSubject<Void, Never>()
}
Then @Published properties stop refreshing Views that depends on this ObservableObject!
Why is that. I also tried to add didSet with objectWillChange.send() but this also causes some odd behaviour and code is a little bit awkward.
Does this mean that I can only use ONLY @Published or ONLY objectWillChange approach?
objectWillChangeand use instead inherited fromObservableObjectindidSet, ie.didSet { self.objectWillChange.send() }updates view as expected. Just in case.