Search code examples
swiftxcodecore-dataswift4ios11

Swift 4 in Xcode 9 - How to lightweight Core Data migration?


My Core Data will update one more attribute and , to avoid crashing , I added a new model version as first, and furthermore: 👇👇👇

The most of the keys about this issue is that change :

coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)

options: nil in the code to

options:[NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption: true]

But In my appdelegate.swift , I can’t find any “persistentStoreCoordinator”, so can I migrate CoreData in my version?


Solution

  • You can achieve like this:
    
    let container = NSPersistentContainer(name: "YourDbModelName")
    let description = NSPersistentStoreDescription() 
    description.shouldMigrateStoreAutomatically = true 
    description.shouldInferMappingModelAutomatically = true 
    container.persistentStoreDescriptions = [description]