简体   繁体   中英

Trying to update my DBModel with lightweight migration but nothing happens

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? 在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM