如何在Swift中进行轻量级CoreData迁移

戴夫G

我正在尝试首次轻量级CoreData迁移。我阅读了有关轻量级迁移的两个指南。两者都向CoreDataStack类添加了代码,修改了诸如NSPersistentStoreCoordinator之类的变量,并添加了:

let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
    NSInferMappingModelAutomaticallyOption: true]

我的问题是我有一个使用CoreData的功能完善的应用程序,但没有该类或类似的东西。我的问题是,为什么这些项目假设我有这个课程,而没有它,我可以实现我的轻量级迁移吗?如果没有,我该如何添加?

更多信息,如果需要回答

9月,我使用CoreData构建了一个应用程序。这是我第一次使用CoreData,并且遵循了Ray Wenderlich指南。效果很好,我完成了该应用程序,现在它已在商店中。现在,我想开始对应用程序进行一些更改,其中涉及新的CoreData属性和一些新的实体。我读到我需要设置一个新的模型版本。

我找到了Ray Wenderlich指南,但是它使用了我没有的CoreDataStack.swift文件:

CoreData类别

令人沮丧的是,我使用他们的指南设置了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]
蒙迪

迁移选项需要在将持久性存储添加到持久性存储协调器的调用中使用。通过搜索,您可以轻松找到这一行代码addPersistentStoreWithType

try coordinator!.addPersistentStoreWithType(
     NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions)

您的核心数据堆栈很可能在AppDelegate类中,但是无论它在哪里,都必须在其中添加迁移选项。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Coredata 轻量级迁移丢失数据

来自分类Dev

轻量级迁移完成通知

来自分类Dev

集成RestKit和MagicalRecord时的CoreData轻量级迁移错误

来自分类Dev

即使添加了映射模型,CoreData轻量级迁移也会崩溃

来自分类Dev

轻量级Core Data迁移后崩溃

来自分类Dev

如何在Swift中进行“深层复制”?

来自分类Dev

如何在Swift中进行子串

来自分类Dev

如何在Swift中进行排序?

来自分类Dev

我如何在Swift中进行@Binding

来自分类Dev

如何在Swift中进行通用扩展?

来自分类Dev

如何在Swift中进行URL调用

来自分类Dev

如何在Swift中进行自省?

来自分类Dev

如何在Swift中进行全局化?

来自分类Dev

如何在Swift中进行子串

来自分类Dev

当我们使用“不存在”时,轻量级交易如何进行?

来自分类Dev

如何列出所有轻量级标签?

来自分类Dev

OrientDB:如何删除特定的轻量级边缘?

来自分类Dev

如何创建更轻量级的片段?

来自分类Dev

iCloud Core Data轻量级迁移-实体消失

来自分类Dev

轻量级迁移时我的应用程序崩溃

来自分类Dev

轻量级迁移时我的应用程序崩溃

来自分类Dev

核心数据-轻量级迁移不起作用

来自分类Dev

Cassandra如何在裂脑模式下处理轻量级事务?

来自分类Dev

iOS8.1核心数据轻量级+重量级迁移

来自分类Dev

如何在Swift 2.0(Xcode 7.2)中进行HTTP请求

来自分类Dev

如何在Realm Swift中进行嵌套查询?

来自分类Dev

如何在Swift中进行字符串编程

来自分类Dev

如何在 iOS swift4 中进行名字验证?

来自分类Dev

如何在数据库优先方法中进行迁移