View with @FetchRequest doesn't update when items added/removed from another device

I thought that if a View had
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.
Answered by DelawareMathGuy in 670921022
hi,

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
Accepted Answer
hi,

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
Thanks, DelawareMathGuy! With those code changes, the Mac and iOS views update automatically. :)
View with @FetchRequest doesn't update when items added/removed from another device
 
 
Q