简体   繁体   English

当我在不使用任何迁移的情况下向数据模型添加/修改新属性时,应用程序不会崩溃(IOS 10+)

[英]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: 核心数据在其自动支持的更改中具有令人惊讶的灵活性,其中包括:

  • addition and removal of attributes 添加和删​​除属性
  • renaming things 重命名
  • changes to optionality 改变可选性
  • changes to relationship types 关系类型的变化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS 10以上版本中,有什么方法可以可靠地唤醒应用程序 - In iOS 10+, is there ANY way to RELIABLY wake up an app 将新模型添加到.xcdatamodeld文件时,是否需要核心数据迁移 - Do i need a core data migration when i add a new model to my .xcdatamodeld file 将应用程序数据迁移到新的IOS应用程序 - Migration of App data to New IOS App 由于核心数据迁移,iOS应用在启动屏幕上崩溃 - iOS app crashing on launch screen due to core data migration 核心数据迁移:我添加的每个新模型版本都需要一个新的映射模型吗? - Core Data Migration: Do I Need a new Mapping Model for each new Model Version I Add? 如何在集合视图中添加 indexTitles? (iOS 10+) - How to add indexTitles in a collection view? (iOS 10+) iOS,reloadRowsAtIndexPaths-将新对象添加到其数据源后崩溃 - iOS, reloadRowsAtIndexPaths - Crashing after i add new object to its Data Source 在标签栏iOS 6.0中打开新标签页时,我的应用程序崩溃了吗? - My App crashing when i open new tab in tabbar iOS 6.0? 在核心数据中更改托管对象模型的属性值时崩溃 - Crashing when Changing Attribute Value of Managed Object Model in Core Data 应用程序崩溃,没有任何错误 - app is crashing without any error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM