我刚刚在 Core Data 上设置了一个只读数据存储。它可以工作,但编译器对此有提示,我想知道如何解决这个问题。
这里的相关代码是我在Xcode自动生成模板后修改的。
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "MyApp")
let appName: String = "MyApp",
storeUrl = Bundle.main.url(forResource: appName, withExtension: "sqlite")
var persistentStoreDescriptions: NSPersistentStoreDescription
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
description.url = storeUrl
container.persistentStoreDescriptions = [description]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
现在这是我从编译器得到的消息:
[error] CoreData: error: Attempt to add read-only file at path ...../MyApp.app/MyApp.sqlite read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption.
如何设置此选项以使编译器满意?
最佳答案
在描述中设置选项
description.setOption(NSNumber(value: true), forKey: NSReadOnlyPersistentStoreOption)
顺便说一句:变量
persistentStoreDescriptions
未使用。
关于ios - NSPersistentContainer/核心数据/只读存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46022820/