I am trying to access a database using Core Data inside a today extension widget.
The function that creates the Core Data container is this:
func createContainer(completion: @escaping (NSPersistentContainer) -> ()) {
let applicationGroupIdentifier = "group.com.name.exampleApp.sharedContainer"
guard let newURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: applicationGroupIdentifier)
else { fatalError("Could not create persistent store URL") }
let persistentStoreDescription = NSPersistentStoreDescription()
persistentStoreDescription.url = newURL
let container = NSPersistentContainer(name: "ContainerName")
container.persistentStoreDescriptions = [persistentStoreDescription]
container.loadPersistentStores { (_, error) in
guard error == nil else { fatalError("Failed to load store:\(String(describing: error))") }
DispatchQueue.main.async { completion(container) }
}
}
However, I get the following error:
CoreData: error: Attempt to add read-only file at path file:///private/var/mobile/Containers/Shared/AppGroup/C9324B65B-265C-4264-95DE-B5AC5C9DAFD0/ read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption.
If I specify the NSReadOnlyPersistentStoreOption like this:
persistentStoreDescription.isReadOnly = false
I get the error:
Failed to load store:Optional(Error Domain=NSCocoaErrorDomain Code=513 "The file couldn’t be saved because you don’t have permission."):
What might be the cause and the solution?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With