NSPersistentContainer等效于NSPersistentStoreCoordinator.addPersistentStore ofType和选项

Gio*_*zzi 6 core-data ios swift3 ios10

在WWDC2016上Apple引入NSPersistentContainer了iOS10

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

它的初始化非常简单:

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

以前在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])
Run Code Online (Sandbox Code Playgroud)

在这种情况下使用

NSSQLiteStoreTypefor ofType 参数

[NSPersistentStoreFileProtectionKey:FileProtectionType.complete, NSMigratePersistentStoresAutomaticallyOption: true] for options 参数

如何配置这种东西NSPersistentContainer

Har*_*nda 11

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

  • 编译器抱怨“FileProtectionType.complete”并要求我转换为“NSObject”这样可以吗?`description.setOption(FileProtectionType.complete as NSObject, forKey: NSPersistentStoreFileProtectionKey)` (2认同)