我正在尝试我的第一个轻量级CoreData迁移.我阅读了两本关于轻量级迁移的指南.两者都将代码添加到CoreDataStack类,修改NSPersistentStoreCoordinator等变量并添加:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,NSInferMappingModelAutomaticallyOption: true]
我的问题是我有一个功能完善的应用程序使用CoreData,但我没有那个类或类似的东西.我的问题是,为什么这些项目假设我有这个课程,我可以在没有它的情况下实现轻量级迁移吗?如果没有,我该如何添加?
更多信息,如果需要回答
九月份,我使用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类,我会添加轻量级代码,例如:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,NSInferMappingModelAutomaticallyOption: true]
解决方法
需要在将持久性存储添加到持久性存储协调器的调用中使用迁移选项.您可以通过搜索addPersistentStoreWithType轻松找到这行代码.
try coordinator!.addPersistentStoreWithType( NSsqliteStoreType,options: mOptions)
很可能您的Core Data堆栈位于AppDelegate类中,但无论它在何处,您都必须添加迁移选项.