Trying to update my DBModel with lightweight migration but nothing happens

124 Views Asked by At

So the categories from my db model got updated ( before there was like 4 now they are 7) and I want to update the core data as well with the new objects.

I tried to apply this : https://developer.apple.com/documentation/coredata/using_lightweight_migration

    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.appendingPathComponent("x.sqlite")
    let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
    var failureReason = "There was an error creating or loading the application's saved data."
    do {
        try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
    } catch {
        // Report any error we got.}

But my data is the same...it s not updating with the new categories. Any help ? Thanks.. PS: Should I make a new model and map the old one in the new one?enter image description here

2

There are 2 best solutions below

0
On

You just forget add another model, you can't make migration between models, if you have only one.So just select your xcdatamodel then open Editor -> add Model Version then go to File Inspector and select new xcdatamodel as current, then you get result. Good luck dear friend

0
On

You need to create the DB model version of CORE DATABASE. You can follow the steps of creating versioning of database.