This what I use in my apps (during the initialisation of my Data Controller):
self.mainContainer = {
let container = NSPersistentCloudKitContainer(name: "MyDataModel")
container.loadPersistentStores(completionHandler: { description, error in
if let error = error {
print("**** ERROR loading persistent store \(error)")
}
//Setup auto merge of Cloudkit data
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
//Set the Query generation to .current. for dynamically updating views from Cloudkit
try? container.viewContext.setQueryGenerationFrom(.current)
})
return container
}()
The key lines are the 2 below //Setup auto merge.
Also, be sure to have enabled Remote Notifications in Background modes of the App's Signing and Capabilities.
I hope this helps. Regards, Michaela