尝试通过轻量级迁移来更新我的DBModel,但没有任何反应

时间:2018-08-27 11:24:52

标签: ios swift core-data core-data-migration lightweight-processes

因此,我的数据库模型中的类别已更新(在出现4之前,现在为7),我也想用新对象更新核心数据。

我尝试应用此方法:https://developer.apple.com/documentation/coredata/using_lightweight_migration

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
    // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.appendingPathComponent("x.sqlite")
    let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
    var failureReason = "There was an error creating or loading the application's saved data."
    do {
        try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
    } catch {
        // Report any error we got.}

但是我的数据是相同的...不会使用新类别进行更新。 有什么帮助吗?谢谢.. PS:我应该建立一个新模型并在新模型中映射旧模型吗?enter image description here

2 个答案:

答案 0 :(得分:1)

您只是忘记添加另一个模型,如果只有一个模型,则无法在模型之间进行迁移,因此只需选择xcdatamodel然后打开Editor-> add Model 版 然后转到File Inspector并选择新的xcdatamodel作为当前版本,然后得到结果。亲爱的朋友,祝你好运

答案 1 :(得分:0)

您需要创建CORE DATABASE的数据库模型版本。您可以按照创建数据库版本控制的步骤进行操作。