[英]App not crashing when i add/Modify new attribute to the data model without using any Migration (IOS 10+)
I have surfed the internet a lot. 我已经上网了很多。 but didn't get any satisfactory answer.
但没有得到满意的答复。
My questions is I'm using Core data with persistent container (iOS 10+).I have released the first version of the app with some data model.later i have added new attribute into old data model entity then rerun the app on top of old data in iPhone without carrying any migrations.But app not crashing and not even logging any errors. 我的问题是我正在使用带有持久性容器(iOS 10+)的Core数据。我已经发布了具有某些数据模型的应用程序的第一个版本。后来我在旧数据模型实体中添加了新属性,然后在iPhone中的旧数据而不进行任何迁移。但是应用程序不会崩溃,甚至不会记录任何错误。
According to documents if any changes to data model makes incompatible with persistent store have to carry out Migrations otherwise app breaks rite away. 根据文档,如果对数据模型的任何更改使得与持久性存储不兼容,则必须进行迁移,否则应用程序中断。
Here is my code, let me know any thing I'm missing. 这是我的代码,让我知道我想念的任何东西。
Intializing Coredata stack 初始化Coredata堆栈
import Foundation
import CoreData
class DatabaseController{
// MARK: - Core Data stack
static var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Test")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
abort()
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
static func saveContext () {
let context = DatabaseController.persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
Storing Sample Data 存储样本数据
let managedObjectContext = DatabaseController.persistentContainer.viewContext
let employee = NSEntityDescription.insertNewObject(forEntityName: "University", into: managedObjectContext) as! University
employee.testName = "Check"
employee.age = "21"
employee.createdDate = "21-2-2019"
do{
try managedObjectContext.save()
}
catch let error{
print(error)
}
Retrieving Sample Data 检索样本数据
do{
let universityEmployeeFetch = NSFetchRequest<NSManagedObject>(entityName: "University")
let fetchedRecords = try managedObjectContext.fetch(universityEmployeeFetch) as! [University]
for employee in fetchedRecords{
if let name = employee.testName,let age = employee.age, let createdDate = employee.createdDate{
print(name,age,createdDate)
}
}
}catch let error{
fatalError()
}
Data Model looks like this where in marked one was the new added atribute 数据模型如下所示,其中标记为一个的是新添加的属性
A quick look at your code suggests that Core Data is able to perform a lightweight migration automatically. 快速查看您的代码表明Core Data能够自动执行轻量级迁移 。
From the link: 从链接:
If you just make simple changes to your model (such as adding a new attribute to an entity), Core Data can perform automatic data migration, referred to as lightweight migration .
如果仅对模型进行简单的更改(例如向实体添加新属性),则Core Data可以执行自动数据迁移,称为轻量级迁移 。
Core Data Model Versioning and Data Migration - Apple Documentation
核心数据模型版本控制和数据迁移-Apple文档
Core Data can be surprisingly flexible in the changes it supports automatically, including: 核心数据在其自动支持的更改中具有令人惊讶的灵活性,其中包括:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.