Using NSPersistentContainer with AppGroups

(As of iOS10 beta 2)


I would like to try the new NSPersistentContainer class to set up Core Data for my app and SiriKit extension, but I'm not sure that's possible. The documentation for is quite sparse at the moment, but it appears we are not able to load an sqlite from an app group usng this new class. Normally I would load MyData.sqlite it using containerURLForSecurityApplicationGroupIdentifier, but all we have to go on is persistentContainerWithName:managedObjectModel: .


Am I missing something?


Thanks in advance!

From what I have gathered, the new method for setting options for the persistent store is by adding persistentStoreDescriptions to the container with the new NSPersistentStoreDescription class. This class has a url method which I presume would be the way to then set the url to your containerURLForSecurityApplicationGroupIdentifier url.


Here is the docs for NSPersistentStoreDescription: https://developer.apple.com/reference/coredata/nspersistentstoredescription

Yup this seems to work...


let container = NSPersistentContainer(name: "Data")      
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: try FileManager.default.containerURLForSecurityApplicationGroupIdentifier("your.group.identifier")!.appendingPathComponent("Data.sqlite"))]
container.loadPersistentStores { (persistentStoreDescription, error) in
   // do things!
}


Alternatively, you seem to be able to subclass NSPersistentContainer and change the location this way:


override public class func defaultDirectoryURL() -> URL {
     let url = FileManager.default.containerURLForSecurityApplicationGroupIdentifier("your.group.name")!
    return url
}


Which I think i slightly prefer..

Using NSPersistentContainer with AppGroups
 
 
Q