Ios 如何解决核心数据错误:在尝试访问app extension today小部件中的核心数据容器时,尝试在路径处添加只读文件?
我正在尝试使用today扩展小部件中的核心数据访问数据库 创建核心数据容器的功能如下:Ios 如何解决核心数据错误:在尝试访问app extension today小部件中的核心数据容器时,尝试在路径处添加只读文件?,ios,core-data,ios10,Ios,Core Data,Ios10,我正在尝试使用today扩展小部件中的核心数据访问数据库 创建核心数据容器的功能如下: func createContainer(completion: @escaping (NSPersistentContainer) -> ()) { let applicationGroupIdentifier = "group.com.name.exampleApp.sharedContainer" guard let newURL = FileManager.default.co
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:错误:尝试在路径处添加只读文件
file:///private/var/mobile/Containers/Shared/AppGroup/C9324B65B-265C-4264-95DE-B5AC5C9DAFD0/
读/写。将其添加为只读。这将是一个严重的错误
未来,;必须指定nsreadonlypersistentstore选项
如果我这样指定nsreadonlypersistentstore选项:
persistentStoreDescription.isReadOnly = false
我得到一个错误:
未能加载存储:可选(错误域=NSCOCAERRORROMAIN代码=513
“无法保存文件,因为您没有权限。”):
可能的原因和解决方案是什么?有相同的问题,通过添加
let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.identifier")!
let url = directory.appendingPathComponent("MyDatabaseName.sqlite")
CoreData可能试图将基本文件夹作为其sqlite数据库打开,但效果并不理想
添加isReadOnly也不允许CoreData重新调整文件夹的用途:)
资料来源:
我使用的源与您相同,但在不同的扩展(UnwantedCommunicationsExtension)上。当我试图从扩展写入时,仍然会遇到相同的错误itself@tink所以
file:///private/var/mobile/Containers/Shared/AppGroup/C9324B65B-265C-4264-95DE-B5AC5C9DAFD0/
:这是您的错误的一部分吗?每个扩展都有不同的隐私捕获集。我正在使用的(不需要的通信报告)不允许从扩展部分写入任何内容,甚至不允许从NSUserDefaults写入任何内容。。我尝试了很多事情,在某个时候我也遇到了你提到的错误。我想不起来我是怎么修的。。对不起@叮叮铃总是发生在我身上。:)所以,尽管你不能写,你还是让它写了下来,还是你只是避免了?那听起来是个有趣的故事,不值得一试。。即使我绕过了一些“如何”,他们也会拒绝这个应用程序。所以我完全改变了我的后端。我想将所有报告的消息存储在核心数据中,并在应用程序中显示报告历史记录,但现在我将UUID传递到后端,并在应用程序中调用检索它。警告:你必须先打开应用程序至少一次,然后再做其他事情。(先让用户接受条款,然后在后台存储用户默认设置中所需的任何内容)底线:您可以将您想要的任何内容从应用程序传递到扩展程序,但不能反过来传递。