简体   繁体   English

核心数据迁移:改变类型的持久化存储

[英]Core Data Migration: Changing Persistent Store ofType

I really need help to change Core Data structure.我真的需要帮助来改变核心数据结构。 I have built my app already with existing database but now I would like to use Seam cloudKit sync and I have to change my code but if I do that my app will crash!我已经使用现有数据库构建了我的应用程序,但现在我想使用 Seam cloudKit 同步,我必须更改我的代码,但如果我这样做,我的应用程序将崩溃! (delete and build the app again is not an option.. as I mention it has already its data structure) (删除并再次构建应用程序不是一种选择......正如我所提到的,它已经有了它的数据结构)

In NSPersistentStoreCoordinator I used the following code:NSPersistentStoreCoordinator我使用了以下代码:

try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: [NSMigratePersistentStoresAutomaticallyOption: true,NSInferMappingModelAutomaticallyOption: true])

Now I have to use that:现在我必须使用它:

try coordinator.addPersistentStore(ofType: SMStore.type, configurationName: nil, at: url, options: [NSMigratePersistentStoresAutomaticallyOption: true,NSInferMappingModelAutomaticallyOption: true]) as? SMStore

As you noticed I have to change the ofType from NSSQLiteStoreType to SMStore.type正如您所注意到的,我必须将 ofType 从NSSQLiteStoreType更改为SMStore.type

Is there any way to do that?有没有办法做到这一点? Is anyone has any idea?有没有人有任何想法? I hope someone could help.我希望有人可以提供帮助。

Thank you very much.非常感谢。

You can use the NSPersistentStoreCoordinator method migratePersistentStore(_:to:options:withType:) for this. NSPersistentStoreCoordinator您可以使用NSPersistentStoreCoordinator方法migratePersistentStore(_:to:options:withType:) This method moves an existing persistent store file to a new file, and can change the persistent store type.此方法将现有的持久存储文件移动到新文件,并且可以更改持久存储类型。 Even though it has "migrate" in the name, it doesn't have anything to do with model version migration.尽管名称中有“迁移”,但它与模型版本迁移没有任何关系。 In this case it migrates the persistent store to a new file.在这种情况下,它将持久存储迁移到一个新文件。

You'll have to add the persistent store using the old store type and then migrate to the new store type.您必须使用旧的存储类型添加持久存储,然后迁移到新的存储类型。

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

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