简体   繁体   English

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

[英]Trying to update my DBModel with lightweight migration but nothing happens

So the categories from my db model got updated ( before there was like 4 now they are 7) and I want to update the core data as well with the new objects. 因此,我的数据库模型中的类别已更新(在出现4之前,现在是7),我也想用新对象更新核心数据。

I tried to apply this : https://developer.apple.com/documentation/coredata/using_lightweight_migration 我试图应用这个: 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.}

But my data is the same...it s not updating with the new categories. 但是我的数据是相同的...不会使用新类别进行更新。 Any help ? 有什么帮助吗? Thanks.. PS: Should I make a new model and map the old one in the new one? 谢谢.. PS:我应该制作一个新模型并将旧模型映射到新模型中吗? 在此处输入图片说明

You just forget add another model, you can't make migration between models, if you have only one.So just select your xcdatamodel then open Editor -> add Model Version then go to File Inspector and select new xcdatamodel as current, then you get result. 您只是忘记添加另一个模型,如果只有一个模型,则无法在模型之间进行迁移,因此只需选择xcdatamodel,然后打开Editor-> add Model Version然后转到File Inspector并选择新的xcdatamodel作为当前模型,则得到结果。 Good luck dear friend 祝你好运亲爱的朋友

You need to create the DB model version of CORE DATABASE. 您需要创建CORE DATABASE的数据库模型版本。 You can follow the steps of creating versioning of database. 您可以按照创建数据库版本控制的步骤进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM