I have a SwiftUI app with CoreData+CloudKit integrated. My data model is as follows:
- MainElement <->> SubElement <-> SubElementMetadata
EachMainElement
has one or moreSubElements
and eachSubElement
has exactly oneSubElementMetadata
.
Now I have a SwiftUI view to display the list of SubElements
of a MainElement
:
struct SubElementListView: View {
@ObservedObject private var mainElement: MainElement
init(mainElement: MainElement) {
self.mainElement = mainElement
}
var body: some View {
List(mainElement.subElements, id: \.self.objectID) { subElement in
VStack {
Text(subElement.title)
Text(subElement.elementMetadata.creationDateString)
}
}
}
}
The Issue:
When I update creationDateString
of a SubElement
somewhere in my code, the view updates to reflect the change. If, however, a change arrives over CloudKit, the view does not update.
If I navigate out and back into the view, the new data is displayed, but not while I stay on this view.
Why is this? I am aware I can probably trigger an update manually (e.g. by listening to CloudKit notifications), but I would like to understand why it doesn't "just work" - which is what I would have expected since NSManagedObject is also an ObservableObject.