Xcode 9中的Swift 4-如何进行轻量级的Core Data迁移?

时间:2018-07-25 12:58:27

标签: swift xcode core-data swift4 ios11

我的核心数据将再更新一个属性,为避免崩溃,我首先添加了一个新的模型版本,此外:

有关此问题的大多数关键在于更改:

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

选项:代码中没有

options:[NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption: true]

但是在我的 appdelegate.swift 中,我找不到任何“ persistentStoreCoordinator” ,所以我可以在我的版本中迁移CoreData吗?

2 个答案:

答案 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]