我的核心数据将再更新一个属性,为避免崩溃,我首先添加了一个新的模型版本,此外:
有关此问题的大多数关键在于更改:
coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
选项:代码中没有
options:[NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption: true]
但是在我的 appdelegate.swift 中,我找不到任何“ persistentStoreCoordinator” ,所以我可以在我的版本中迁移CoreData吗?
答案 0 :(得分:2)
Gulshan Kumar的解决方案很好。在我的情况下,它不起作用,因为容器已经有一个描述对象,并且设置了
container.persistentStoreDescriptions = [description]
有效地删除了该对象,结果模型没有加载。我用过
container.persistentStoreDescriptions.append(description)
答案 1 :(得分:1)
You can achieve like this:
let container = NSPersistentContainer(name: "YourDbModelName")
let description = NSPersistentStoreDescription()
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions = [description]