如何在Swift中进行轻量级CoreData迁移 [英] How to Do a Lightweight CoreData Migration in Swift
问题描述
我正在尝试第一次轻量级CoreData迁移。我阅读了两本关于轻量级迁移的指南。两者都将代码添加到CoreDataStack类,修改NSPersistentStoreCoordinator等变量并添加:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption:true,
NSInferMappingModelAutomaticallyOption:true]
我的问题是我有一个功能完善的应用程序使用CoreData但我不知道有那个班级或类似的东西。 我的问题是,为什么这些项目假设我有这个课程,我可以在没有它的情况下实现轻量级迁移吗?如果没有,我该如何添加?
更多信息,如果需要回答
9月份,我使用CoreData构建了一个应用程序。这是我第一次使用CoreData,我跟着这个Ray
令人沮丧的是,我使用他们的指南设置了CoreData,但它没有包含该文件!然后我去做迁移,他们假设我有它。
我去寻找另一个轻量级迁移方法,发现这个替代,它也引用了我从未构建到我的CoreData中的代码:
lazy var persistentStoreCoordinator:NSPersistentStoreCoordinator? = {
//应用程序的持久性存储协调器。此实现创建并返回协调器,已将应用程序的存储添加到其中。此属性是可选的,因为存在可能导致存储创建失败的合法错误条件。
//创建协调器并存储
var协调器:NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel:self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(MyLog.sqlite)
var error:NSError? = nil
var failureReason =创建或加载应用程序保存的数据时出错。
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType,configuration:nil,URL:url,options:nil,error:& error)== nil {
coordinator = nil
//报告任何错误我们有。
var dict = [String:AnyObject]()
dict [NSLocalizedDescriptionKey] =无法初始化应用程序保存的数据
dict [NSLocalizedFailureReasonErrorKey] = failureReason
dict [NSUnderlyingErrorKey] = error
error = NSError(domain:YOUR_ERROR_DOMAIN,代码:9999,userInfo:dict)
//用代码替换它以适当地处理错误。
// abort()导致应用程序生成崩溃日志并终止。您不应在运输应用程序中使用此功能,尽管它在开发期间可能很有用。
NSLog(未解决的错误\(错误),\(错误!.userInfo))
abort()
}
所以我已经阅读了指南并了解了90%的教程。我只需要有人看看原始的CoreData教程并告诉我在哪里,如果我没有CoreData类,我会添加轻量级代码,例如:
让mOptions = [NSMigratePersistentStoresAutomaticallyOption:true,
NSInferMappingModelAutomaticallyOption:true]
需要在将持久性存储添加到持久性存储协调器的调用中使用迁移选项。您可以通过搜索 addPersistentStoreWithType
轻松找到这行代码。
尝试协调器!.addPersistentStoreWithType(
NSSQLiteStoreType,配置:nil,URL:url,options:mOptions)
很可能你的核心数据堆栈在AppDelegate类中,但无论它在哪里,这都是你拥有的地方添加迁移选项。
I am attempting my first lightweight CoreData migration. I read two guides on lightweight migrations. Both add code to a CoreDataStack class, modifying variables like NSPersistentStoreCoordinator and adding:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
My problem is that I have a perfectly functioning app using CoreData but I don't have that class or anything like it. My problem is, why do these projects assume I have this class and can I achieve my light weight migration without it? If not, how do I add it?
More Info, If Needed to Answer
In September I built an app using CoreData. It was my first time using CoreData and I followed this Ray Wenderlich guide. It worked great, I finished the app, and its now on the store. Now I'd like to start making some changes to the app, which involve new CoreData attributes and a few new entities. I've read that I need to setup a new model version.
I found a Ray Wenderlich guide but it uses this CoreDataStack.swift file that I don't have:
What's frustrating is that I setup CoreData using their guide and it didn't include that file! Then I go to do a migration and they assume I have it.
I went in search of another lightweight migration method, found this alternative and it too references code that I never built into my 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()
}
So I've read the guides and understand 90% of the tutorials. I just need someone to take a look at that original CoreData tutorial and tell me where, if I don't have a CoreData class, I would add the lightweight code such as:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
The migration options need to be used in the call that added the persistent store to the persistent store coordinator. You will easily find this line of code by doing a search for addPersistentStoreWithType
.
try coordinator!.addPersistentStoreWithType(
NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions)
Most likely your Core Data stack is in the AppDelegate class, but regardless where it is, this is where you have to add the migration options.
这篇关于如何在Swift中进行轻量级CoreData迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!