My app uses SwiftData and CloudKit to store and synchronize data. This works nice. in some scenarios, I need to listen to NSPersistentStoreRemoteChange
event to deduplicate data.
In SwiftData, how do I get the persistentStoreCoordinator of the corresponding SwiftData ModelContainer? Or are there other APIs to achieve the same purpose? There is no relevant content in the developer documentation.
In addition, in wwdc 24, there is a new API to track history (https://developer.apple.com/cn/videos/play/wwdc2024/10075/). But there is no mention of how to listen to remote data change events either.
SwiftData + CloudKit turns on NSPersistentStoreRemoteChangeNotificationPostOptionKey
automatically, and you can observe the .NSPersistentStoreRemoteChange
notification in the same way as you do with Core Data. I provide a code example in this post to demo how to observe a Core Data notification in SwiftUI + SwiftData. You can replace .NSManagedObjectContextDidSave
with .NSPersistentStoreRemoteChange
in the code example to observe the remote change notifications.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.