ios - 核心数据-根据应用程序版本不同的数据

标签 ios swift xcode core-data

我有一个使用标准核心数据堆栈的 Swift 3 IOS 10 应用程序。我的一些用户报告在升级到最新版本的应用程序时丢失了数据。似乎正在发生的情况是,即使模型没有更改,并且没有迁移,也会创建一个新的数据存储。

我可以通过显示已发布的版本数据集 1 来重新创建它。但是当我将开发版本推送到我的设备时,数据集 1 消失了。我可以向数据集 2 添加一条记录,它仍然保持正常。但如果我返回应用商店并获取已发布的版本,数据集 1 会再次出现。

有人可以解释为什么会发生这种情况并防止它发生吗?我错过了一些简单的事情吗?提前致谢...

最佳答案

我遇到了类似的问题,当我更改 AppDelegate 中的这一部分时,它为我修复了它。

 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
    // The persistent store coordinator for the application. This implementation creates and returns 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
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
    var failureReason = "There was an error creating or loading the application's saved data."
    do {
        try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: [NSMigratePersistentStoresAutomaticallyOption: true,
            NSInferMappingModelAutomaticallyOption: true]) //THIS LINE
    } catch {
        // 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 as NSError
        let wrappedError = 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 \(wrappedError), \(wrappedError.userInfo)")
        abort()
    }

    return coordinator
}()

关于ios - 核心数据-根据应用程序版本不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648215/

相关文章:

ios - Apple Watch 应用检测 Apple Watch 是否与手机配对

swift - 是否可以将 Enum 值与 'if' 语句进行比较?

ios - 收到错误 "Type ViewController does not conform to protocol ' UITableViewDataSource ...“即使我有所需的功能

swift - 负数减法,正数加法

ios - ScrollView 、 Collection View 和一页控件

c++ - Xcode C++ 错误阻止进一步输出超过 srand()

ios - UIButton 在 UIPopoverController 中不起作用

ios - 围绕一个点旋转的一步仿射变换?

ios - 将值设置为 SWIFT 中的计算属性

在 OS X Swift 应用程序中单击按钮时 Xcode 打印 "Hello World"的方法?