persistentStoreDescriptions 放在哪里?在 Swift3 Coredata 样板堆栈中
where to put persistentStoreDescriptions ? in Swift3 Coredata Boilerplate Stack
我将我的 Coredata 迁移到新版本,并将 persistentStoreDescriptions
在 loadPersistentStores 和应用程序不会崩溃但不会保留数据之前,考虑到它在迁移之前运行良好,这是我的代码:
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "CoreTest3")
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
container.persistentStoreDescriptions = [description]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
根据文档,您必须在初始化持久存储描述时指定 URL 托管对象模型:
let description = NSPersistentStoreDescription(url: storeURL)
要获得 URL,请查看 Bundle.URL(forResource:withExtension)
我将我的 Coredata 迁移到新版本,并将 persistentStoreDescriptions 在 loadPersistentStores 和应用程序不会崩溃但不会保留数据之前,考虑到它在迁移之前运行良好,这是我的代码:
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "CoreTest3")
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
container.persistentStoreDescriptions = [description]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
根据文档,您必须在初始化持久存储描述时指定 URL 托管对象模型:
let description = NSPersistentStoreDescription(url: storeURL)
要获得 URL,请查看 Bundle.URL(forResource:withExtension)