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

心不动则不痛 提交于 2019-12-08 07:51:31

问题


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?


回答1:


You can achieve like this:

let container = NSPersistentContainer(name: "YourDbModelName")
let description = NSPersistentStoreDescription() 
description.shouldMigrateStoreAutomatically = true 
description.shouldInferMappingModelAutomatically = true 
container.persistentStoreDescriptions = [description]



回答2:


The solution from Gulshan Kumar is good. In my case, it didn't work because the container already had one description object, and setting

container.persistentStoreDescriptions = [description]

effectively deleted that object, with the result that the model did not load. I used

container.persistentStoreDescriptions.append(description)


来源:https://stackoverflow.com/questions/51519516/swift-4-in-xcode-9-how-to-lightweight-core-data-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!