How to solve Core Data error: Attempt to add read-only file at path when trying to access Core Data container in the app extension today widget?
How to solve Core Data error: Attempt to add read-only file at path when trying to access Core Data container in the app extension today widget?
我正在尝试使用 Today 扩展小部件中的 Core Data 访问数据库。
创建核心数据容器的函数是这样的:
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) }
}
}
但是,我收到以下错误:
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.
如果我像这样指定 NSReadOnlyPersistentStoreOption:
persistentStoreDescription.isReadOnly = false
我收到错误:
Failed to load store:Optional(Error Domain=NSCocoaErrorDomain Code=513
"The file couldn’t be saved because you don’t have permission."):
可能是什么原因和解决方法?
遇到同样的问题,添加
解决了
let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.identifier")!
let url = directory.appendingPathComponent("MyDatabaseName.sqlite")
CoreData 可能试图打开基础文件夹作为它的 sqlite 数据库,但并不顺利。
添加 isReadOnly 也不允许 CoreData 重新调整文件夹的用途:)
来源:https://github.com/maximbilan/iOS-Shared-CoreData-Storage-for-App-Groups
- 在扩展的 info.plist 中设置:NSExtension --> NSExtensionAttributes --> RequestOpenAccess --> YES
- 在设备设置中设置:您的应用 -->(扩展类型 - 例如:键盘)--> 允许完全访问
我正在尝试使用 Today 扩展小部件中的 Core Data 访问数据库。
创建核心数据容器的函数是这样的:
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) }
}
}
但是,我收到以下错误:
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.
如果我像这样指定 NSReadOnlyPersistentStoreOption:
persistentStoreDescription.isReadOnly = false
我收到错误:
Failed to load store:Optional(Error Domain=NSCocoaErrorDomain Code=513 "The file couldn’t be saved because you don’t have permission."):
可能是什么原因和解决方法?
遇到同样的问题,添加
解决了let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.identifier")!
let url = directory.appendingPathComponent("MyDatabaseName.sqlite")
CoreData 可能试图打开基础文件夹作为它的 sqlite 数据库,但并不顺利。
添加 isReadOnly 也不允许 CoreData 重新调整文件夹的用途:)
来源:https://github.com/maximbilan/iOS-Shared-CoreData-Storage-for-App-Groups
- 在扩展的 info.plist 中设置:NSExtension --> NSExtensionAttributes --> RequestOpenAccess --> YES
- 在设备设置中设置:您的应用 -->(扩展类型 - 例如:键盘)--> 允许完全访问