我正在尝试我的第一个轻量级CoreData迁移。我读了两本关于轻量级迁移的指南。两种方法都向CoreDataStack类添加代码,修改NSPersistentStoreCoordinator之类的变量,并添加:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]我的问题是,我有一个功能完善的应用程序使用CoreData,但我没有这个类或类似的东西。我的问题是,为什么这些项目假设我有这个类,如果没有它,我可以实现轻量级迁移吗?如果没有,我该如何添加呢?
更多信息,如果需要回答
9月份,我用CoreData开发了一个应用程序。这是我第一次使用CoreData,我遵循了这个Ray 温德利希指南。它运行得很好,我完成了这个应用程序,现在它在商店里。现在,我想开始对这个应用程序做一些修改,包括新的CoreData属性和一些新的实体。我已经读到我需要建立一个新的模型版本。
我找到了一个雷·温德利希指南,但是它使用了我没有的CoreDataStack.swift文件:

令人沮丧的是,我设置了CoreData使用他们的指南,它没有包括该文件!然后我去做一个迁移,他们假设我有。
我寻找了另一种轻量级迁移方法,找到了这个替代方案,它也引用了我从未在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]发布于 2016-01-20 23:55:58
迁移选项需要在将持久存储添加到持久存储协调器的调用中使用。通过搜索addPersistentStoreWithType,您将很容易找到这一行代码。
try coordinator!.addPersistentStoreWithType(
NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions)您的核心数据堆栈很可能在AppDelegate类中,但是不管它在哪里,您都必须在这里添加迁移选项。
https://stackoverflow.com/questions/34912733
复制相似问题