Presently, I am encountering an issue with SwiftData. For instance, I have a SwiftData class Ledger
that encompasses an array of SingleTransaction
, which is also a SwiftData class.
Here is the question: when I save a Ledger
, how can I discern that the .NSManagedObjectContextDidSave
notification was triggered by saving the Ledger
and not by saving a SingleTransaction
? This distinction is crucial to circumvent unnecessary updates. I attempted the following syntax, but Xcode indicates that Cast from NSManagedObject
to unrelated type Ledger
always fails.
List {...}
.onReceive(
NotificationCenter
.default
.publisher(for: .NSManagedObjectContextDidSave)
.receive(on: DispatchQueue.main),
perform: { notification in
if let userInfo = notification.userInfo,
let updatedObjects = userInfo[NSUpdatedObjectsKey] as? Set<NSManagedObject> {
if updatedObjects.contains(where: { $0 is Ledger }) {
fetchLedgers()
}
}
}
)
What can I do?
"This distinction is crucial to circumvent unnecessary updates."
Yeah, avoiding unnecessary UI updates is important. To figure out what change should trigger an update, you can look into the details of the changes provided in the SwiftData history, which was introduced in WWDC2024.
For more information about SwiftData history, please see the Track model changes with SwiftData history session. For a workable sample, see the Detect relevant changes by consuming the SwiftData history section of the Adopting SwiftData for a Core Data app sample.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.