gpt4 book ai didi

ios - 如何解决核心数据错误 : Attempt to add read-only file at path when trying to access Core Data container in the app extension today widget?

转载 作者:行者123 更新时间:2023-11-29 11:45:54 27 4
gpt4 key购买 nike

我正在尝试使用今日扩展小部件中的核心数据访问数据库。

创建核心数据容器的函数是这样的:

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

关于ios - 如何解决核心数据错误 : Attempt to add read-only file at path when trying to access Core Data container in the app extension today widget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43897795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com