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