我有一个使用标准核心数据堆栈的 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/