I have an ObservableObject
with @Published
properties:
class SettingsViewState: ObservableObject {
@Published var viewData: SettingsViewData = .init()
…
I would like to change viewData
to a computed var based on other sources of truth rather than allowing it to be directly modified. However, I still want views looking at viewData
to automatically update as it changes. They should update when properties it is computed from change.
I'm really not very certain about how @Published
actually works though. I think it has its willSet
perform objectWillChange.send()
on the enclosing ObservableObject
before a change occurs, but I'm not sure!
If my suspicion is correct, it seems like I could manually call objectWillChange.send()
on the enclosing object if anything viewData
depends on will change.
Alternatively, if properties viewData
is computed from are themself @Published
, when I change one, presumably an equivalent objectWillChange.send()
will occur automatically, and I won't need to do anything special? This should work even if these properties are private
and a watching view doesn't have access to them: it should still see the objectWillChange
being emitted?
However, It's entirely possible I've got this horribly garbled or mostly backwards! Eg, perhaps the @Published
properties have their own independent change publisher, rather than simply making use of the enclosing ObservableObject
's? Or both of them publish prior to a change?
Clarification will be gratefully received. Thank you!