Search code examples
iosswiftswift4core-data-migration

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.

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

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

enter image description here


Solution

  • A quick look at your code suggests that Core Data is able to perform a lightweight migration automatically.

    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 Model Versioning and Data Migration - Apple Documentation

    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