2

I have successfully launched 1st version of my App with MyProject.xcdatamodel. Now my 2nd version is under development and I created new model version named MyProject2.xcdatamodel based on MyProject.xcdatamodel and set it to Current. I have enabled light-weight migration as following in my AppDelegate:

lazy var persistentContainer: NSPersistentContainer = {

let container = NSPersistentContainer(name: "MyProject")

    // Support for light-weight migration 
    /*-------------------------------------------------------*/
    let description = NSPersistentStoreDescription()
    description.shouldMigrateStoreAutomatically = true
    description.shouldInferMappingModelAutomatically = true
    container.persistentStoreDescriptions = [description]
    /*-------------------------------------------------------*/

    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

After doing this, I first installed my old version, and then installed new version on top of old. While launching new version, there is no data found from one of my database table of old model which I did not have touch. If I remove those 4 lines of migration, it is getting data.

What may be the reason? Is there anything wrong I am doing for light-weight migration?

NSPratik
  • 4,714
  • 7
  • 51
  • 81
  • What are some of the changes you made in your model? Do those changes support lightweight migration? Depending on what you have changed, you may need to add a mapping model between the two versions. – richardpiazza May 07 '20 at 13:55
  • I have added new tables in 2nd model, whithout touching tables of 1st model.. – NSPratik May 07 '20 at 13:59
  • You'll want to be certain that the mappings can be inferred. `NSMappingModel.inferredMappingModel(forSourceModel:desitinationModel:)` can be used to determine that. Additional information can be found at https://developer.apple.com/documentation/coredata/using_lightweight_migration – richardpiazza May 07 '20 at 16:11
  • @nspratik Did you figure out the issue? Having the same problem. – Kani Lana Aug 29 '21 at 03:52

1 Answers1

1

You need to specify the URL for the location of the actual model's database location.

    lazy var persistentContainer: NSPersistentContainer = {

    let container = NSPersistentContainer(name: Constants.yourModelName)

    if let storeURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        .appendingPathComponent("\(Constants.yourModelName).sqlite") {
        /*add necessary support for migration*/
        let description = NSPersistentStoreDescription(url: storeURL)
        description.type = NSSQLiteStoreType
        description.shouldMigrateStoreAutomatically = true
        description.shouldInferMappingModelAutomatically = true
        container.persistentStoreDescriptions =  [description]
    }

    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error {
            print("Unresolved error, \((error as NSError).userInfo)")
        }
    })
    return container
}()