-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Consider the following example:
class ObservableDemoObject: ObservableObject {
@Published var text: String
init(text: String) {
self.text = text
}
var notPublished: Date {
get { Date.now }
set { objectWillChange.send() }
}
}
struct ObservableDemoView: View {
@StateObject var observableDemoObject = ObservableDemoObject(text: "ABC")
var body: some View {
VStack {
Text("notPublished: \(observableDemoObject.notPublished)")
Button("Change notPublished") {
observableDemoObject.notPublished = Date.distantPast // ignored, but should trigger view update
}
.buttonStyle(.borderedProminent)
Text("text: \(observableDemoObject.text)")
Button("Change text") {
observableDemoObject.text = String(observableDemoObject.text.reversed())
}
.buttonStyle(.borderedProminent)
}
.padding()
}
}When pressing the "Change text" button, the view refreshes on both iOS and Android. But pressing "Change notPublished" refreshes on iOS but not Android (as can be seen by the Date not updating on the screen).
It seems that calling objectWillChange.send() is not sufficient to cause a recompose. A work-around (reported on Slack) is to update a bogus internal @Published property rather than calling objectWillChange.send(), which will trigger the recompose.
Metadata
Metadata
Assignees
Labels
No labels