ios - NSPersistentContainer 等效于 NSPersistentStoreCoordinator.addPersistentStore ofType 和选项

标签 ios core-data swift3 ios10

在 WWDC2016 上,Apple 推出了适用于 iOS10 的 NSPersistentContainer

NSPersistentContainer 类负责加载数据模型,创建托管对象模型,并使用它创建 NSPersistentStoreCoordinator。

它的初始化非常简单:

let container = NSPersistentContainer(name: "myContainerName")
container.loadPersistentStores(completionHandler: { /* ... handles the error ... */ })

之前在 CoreData 堆栈创建中,我们设置了 NSPersistentStoreCoordinator 添加一个 PersistentStore,特别是使用“ofType”和“storeOptions”

let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
psc.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: [NSPersistentStoreFileProtectionKey:FileProtectionType.complete, NSMigratePersistentStoresAutomaticallyOption: true] as [NSObject : AnyObject])

在这种情况下使用

NSSQLiteStoreType for ofType 参数

[NSPersistentStoreFileProtectionKey:FileProtectionType.complete, NSMigratePersistentStoresAutomaticallyOption: true] 对于选项参数

如何使用 NSPersistentContainer 配置此类内容?

最佳答案

let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
description.setOption(FileProtectionType.complete, forKey: NSPersistentStoreFileProtectionKey)
container.persistentStoreDescriptions = [description]

关于ios - NSPersistentContainer 等效于 NSPersistentStoreCoordinator.addPersistentStore ofType 和选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42693394/

相关文章:

ios - 如何在 Release模式下启用框架的可测试性?

iphone - 标签栏似乎被锁定(默认设置)

IOS - 来自 NSBundle mainbundle 的 SQLite DB 路径返回 null

cocoa - CoreData模型设计: Is excessive use of NSFetchRequest a symptom of a poorly designed model?

ios - Swift 3 - 自定义相机 View - 显示单击按钮后拍摄的照片的静止图像

ios - 如何在选择 Swift 3.0 时缩放 UICollectionViewCell?

ios - 在 swift 3 中生成你自己的错误代码

iphone - 如何检测iPhone是否震动?

json - 确定用于简单时间跟踪应用程序的数据模型

ios - 重写 ManageObjectContext - 数据消失