ios - NSPersistentContainer/核心数据/只读存储

标签 ios core-data

我刚刚在 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/

相关文章:

core-data - 具有一对多关系的 NSSortDescriptor

ios - 准备 iOS 演示应用程序和主应用程序的最佳实践是什么?

ios - 更改电池附件类型?

iphone - 如何在 xcode 中运行 PJSIP?

ios - 未声明的选择器 goBack 警告和无法识别的选择器发送到实例终止

ios - 核心数据迁移 删除实体数据

ios - 如何使用coredata每次从表中获取10条记录

iphone - 将一个 NSNumber 的 NSSet 变成一个 int 的 NSSet

ios - UITableview 中各部分之间的空间

swift - CoreData 谓词使用关系