How to migrate NSPersistentCloudKitContainer to App Group to access in Widget

I have shipped an app that utilizes Core Data in CloudKit viaNSPersistentCloudKitContainer. I now want to add a widget that can query for the current data to display. It's my understanding you need to migrate this to a new location available to a shared App Group. How do you do this?

container = NSPersistentCloudKitContainer(name: "AppName")

container.loadPersistentStores { description, error in
    //handle error
}

container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

container.viewContext.automaticallyMergesChangesFromParent = true
Answered by Frameworks Engineer in 678426022

You'll need to change the persistent store URL to reside in your group container. For existing customers this will require moving the store file to the new directory before you load it.

See https://developer.apple.com/documentation/coredata/nspersistentstorecoordinator/3747536-replacepersistentstore

Accepted Answer

You'll need to change the persistent store URL to reside in your group container. For existing customers this will require moving the store file to the new directory before you load it.

See https://developer.apple.com/documentation/coredata/nspersistentstorecoordinator/3747536-replacepersistentstore

How to migrate NSPersistentCloudKitContainer to App Group to access in Widget
 
 
Q