作者:负能量包子玻璃包各负磁场宣泄区 | 来源:互联网 | 2023-02-09 15:15
如何解决《NSPersistentContainer等效于NSPersistentStoreCoordinator.addPersistentStoreofType和选项》经验,为你挑选了1个好方法。
在WWDC2016上Apple引入NSPersistentContainer
了iOS10
该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]
for options 参数
如何配置这种东西NSPersistentContainer
?
1> Harshal Vala..:
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
description.setOption(FileProtectionType.complete, forKey: NSPersistentStoreFileProtectionKey)
container.persistentStoreDescriptiOns= [description]