如何在 Swift 中进行轻量级 CoreData 迁移
How to Do a Lightweight CoreData Migration in Swift
我正在尝试我的第一个轻量级 CoreData 迁移。我阅读了两份关于轻量级迁移的指南。两者都将代码添加到 CoreDataStack class,修改 NSPersistentStoreCoordinator 等变量并添加:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
我的问题是我有一个使用 CoreData 的功能完美的应用程序,但我没有 class 或类似的东西。 我的问题是,为什么这些项目假设我有这个 class 并且我可以在没有它的情况下实现我的轻量级迁移?如果没有,如何添加?
更多信息,如果需要回答
9 月,我使用 CoreData 构建了一个应用程序。这是我第一次使用 CoreData,我遵循了这个 Ray Wenderlich 指南。效果很好,我完成了该应用程序,现在已在商店上架。现在我想开始对应用程序进行一些更改,其中涉及新的 CoreData 属性和一些新实体。我读过我需要设置一个新的模型版本。
我找到了 Ray Wenderlich guide,但它使用了我没有的 CoreDataStack.swift 文件:
令人沮丧的是,我使用 他们的 指南设置了 CoreData,但它没有包含该文件!然后我去做一个迁移,他们假设我有它。
我去寻找另一种轻量级迁移方法,找到了这个 alternative 并且它也引用了我从未构建到我的 CoreData 中的代码:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added 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.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyLog.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() 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.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
所以我已经阅读了指南并理解了 90% 的教程。我只需要有人看一下原始的 CoreData 教程并告诉我在哪里,如果我没有 CoreData class,我会添加轻量级代码,例如:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
需要在将持久存储添加到持久存储协调器的调用中使用迁移选项。您可以通过搜索 addPersistentStoreWithType
轻松找到这行代码。
try coordinator!.addPersistentStoreWithType(
NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions)
您的核心数据堆栈很可能在 AppDelegate class 中,但无论它在哪里,您都必须在此处添加迁移选项。
我正在尝试我的第一个轻量级 CoreData 迁移。我阅读了两份关于轻量级迁移的指南。两者都将代码添加到 CoreDataStack class,修改 NSPersistentStoreCoordinator 等变量并添加:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
我的问题是我有一个使用 CoreData 的功能完美的应用程序,但我没有 class 或类似的东西。 我的问题是,为什么这些项目假设我有这个 class 并且我可以在没有它的情况下实现我的轻量级迁移?如果没有,如何添加?
更多信息,如果需要回答
9 月,我使用 CoreData 构建了一个应用程序。这是我第一次使用 CoreData,我遵循了这个 Ray Wenderlich 指南。效果很好,我完成了该应用程序,现在已在商店上架。现在我想开始对应用程序进行一些更改,其中涉及新的 CoreData 属性和一些新实体。我读过我需要设置一个新的模型版本。
我找到了 Ray Wenderlich guide,但它使用了我没有的 CoreDataStack.swift 文件:
令人沮丧的是,我使用 他们的 指南设置了 CoreData,但它没有包含该文件!然后我去做一个迁移,他们假设我有它。
我去寻找另一种轻量级迁移方法,找到了这个 alternative 并且它也引用了我从未构建到我的 CoreData 中的代码:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added 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.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyLog.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() 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.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
所以我已经阅读了指南并理解了 90% 的教程。我只需要有人看一下原始的 CoreData 教程并告诉我在哪里,如果我没有 CoreData class,我会添加轻量级代码,例如:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
需要在将持久存储添加到持久存储协调器的调用中使用迁移选项。您可以通过搜索 addPersistentStoreWithType
轻松找到这行代码。
try coordinator!.addPersistentStoreWithType(
NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions)
您的核心数据堆栈很可能在 AppDelegate class 中,但无论它在哪里,您都必须在此处添加迁移选项。