I am developing an app that use CoreData with targets for iO and tvOS.
I also want to use CoreData with some widgets / extensions.
I create an App Groups to share CoreData between App and extensions for iOS and for tvOS:
group.com.dummy.AppName
I used the same name for the App Gropus for both iOS and tvOS.
I subclass NSPersistentContainer to use as defaultDirectoryURL() the App Group as follow:
class PersistentContainerForAppGroups : NSPersistentContainer {
override class func defaultDirectoryURL() -> URL {
guard let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.dummy.AppName") else { fatalError("Can not get url forSecurityApplicationGroupIdentifier")}
return url
}
}Then I set the persistentContainer as follows:
lazy var persistentContainer: PersistentContainerForAppGroups = {
let container = PersistentContainerForAppGroups(name: "AppName")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()The app works ok in iOS device, iOS simulator, appleTV simulator but not in appleTV device.
I get this error when try to run the app in appleTV device:
error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///private/var/mobile/Containers/Shared/AppGroup/94BF2E01-FE91-4D6F-971B-3915B212B71B/AppName.sqlite options:{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=512 "The file couldn’t be saved." UserInfo={reason=Failed to create file; code = 1} with userInfo dictionary {
reason = "Failed to create file; code = 1”;
}
What is wrong with the above code?