preload core data from cloud kit

I am using the NSPersistentCloudKitContainer for my app's core data. This works well enough on a single device. However, (using the same Apple ID) when I run my app on a second device, the data on cloud kit is ignored. What is the magic code needed to pull the cloud kit data into my app's core data store?

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

preload core data from cloud kit
 
 
Q