我从基于Core数据文档的模板创建了一个新的Swift应用程序。应用程序运行良好,但对于新版本,我想添加轻量级迁移。
在我所阅读的核心数据文档中,我只需在addPersistentStoreWithType:configuration:URL:options:error:
方法中添加一些选项,但实际上并没有提示来调用/添加。
我有Document
类,它是从NSPersistentDocument
派生的,也是应用程序委托的。
addPersistentStoreWithType:configuration:URL:options:error:
调用的方法在哪里?发布于 2016-07-27 04:55:30
它隐藏在NSPersistentDocument
的文档中。
您可以通过重写方法configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:).和NSPersistentDocument来自定义持久性堆栈的体系结构。例如,您可能希望这样做来指定特定的托管对象模型。
覆盖func configurePersistentStoreCoordinator(for url: URL, ofType fileType: String, modelConfiguration configuration: String?, storeOptions: [String : AnyObject]? = [:])
。将您的选项添加到storeOptions
并调用超级。
发布于 2016-07-27 02:37:36
请参阅苹果文档
在Swift中创建选项并调用addPersistentStoreWithType
let options = [NSMigratePersistentStoresAutomaticallyOption:true,NSInferMappingModelAutomaticallyOption:true]
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options)
这是用lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator
在appDelegate:(didFinishLaunchingWithOptions)
中完成的。
编辑说:--这只适用于iOS应用程序,对于基于文档的应用程序,您可以找到答案这里
https://stackoverflow.com/questions/38610035
复制相似问题