2017-09-03 44 views
0

我刚刚在Core Data上设置了只读数据存储。它的工作原理,但编译器有抱怨,我想知道如何解决这个问题。NSPersistentContainer/Core Data/ReadOnly存储

这里的相关代码是我在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. 

如何使编译器很高兴我设置这个选项?

回答

2

设置在描述

description.setOption(NSNumber(value: true), forKey: NSReadOnlyPersistentStoreOption) 

通过道路的选择:变量persistentStoreDescriptions未使用。

+0

是的,它做到了。谢谢。对于persistentStoreDescriptions是真的。 – Michel