2017-03-09 20 views
4

在WWDC2016苹果公司推出NSPersistentContainer为iOS10NSPersistentContainer等效NSPersistentStoreCoordinator.addPersistentStore ofType和期权

NSPersistentContainer类是负责加载数据模型,创建一个管理对象模型,并用它来创建的NSPersistentStoreCoordinator。

它的初始化是很容易的:

let container = NSPersistentContainer(name: "myContainerName") 
container.loadPersistentStores(completionHandler: { /* ... handles the error ... */ }) 
在我们成立了 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]) 

的CoreData堆栈创建

以前在这种情况下,

NSSQLiteStoreType对于的类型参数

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

我如何设置这种使用NSPersistentContainer东西?

回答

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