I thought that if a View had
and the body contained
that that would be enough for insertions and deletions to update the view automatically. Is there more that I need to do?
I created a cross-platform app using the "App" template in Xcode 12.4 this morning. The template creates a model with an Item entity, and a main view that renders a list of all items and their creation timestamps.
In both iOS and macOS targets, I enabled CloudKit, created a container, and enabled background notifications. Then I confirmed that if I opened the app from my phone, I could see items created on my Mac, and vice versa.
However, neither device shows changes (insertions or deletions) made on the other device until the app is restarted.
Code Block @FetchRequest(…) private var items: FetchedResults<Item>
and the body contained
Code Block ForEach(items) { item in … }
that that would be enough for insertions and deletions to update the view automatically. Is there more that I need to do?
Context
I created a cross-platform app using the "App" template in Xcode 12.4 this morning. The template creates a model with an Item entity, and a main view that renders a list of all items and their creation timestamps.
In both iOS and macOS targets, I enabled CloudKit, created a container, and enabled background notifications. Then I confirmed that if I opened the app from my phone, I could see items created on my Mac, and vice versa.
However, neither device shows changes (insertions or deletions) made on the other device until the app is restarted.
hi,
did you set NSPersistentHistoryTrackingKey and NSPersistentStoreRemoteChangeNotificationPostOptionKey, along with automaticallyMergesChangesFromParent and the mergePolicy? i use this code.
there's also a recent article by Becky Hansmeyer (h ttps://beckyhansmeyer.com) that may be helpful.
hope that helps,
DMG
did you set NSPersistentHistoryTrackingKey and NSPersistentStoreRemoteChangeNotificationPostOptionKey, along with automaticallyMergesChangesFromParent and the mergePolicy? i use this code.
Code Block guard let persistentStoreDescriptions = container.persistentStoreDescriptions.first else { fatalError("\(#function): Failed to retrieve a persistent store description.") } persistentStoreDescriptions.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) persistentStoreDescriptions.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) container.loadPersistentStores(completionHandler: { (storeDescription, error) in ... }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
there's also a recent article by Becky Hansmeyer (h ttps://beckyhansmeyer.com) that may be helpful.
hope that helps,
DMG