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

Core Data Migration: Changing Persistent Store ofType

我真的需要帮助来改变核心数据结构。我已经使用现有数据库构建了我的应用程序,但现在我想使用 Seam cloudKit sync,我必须更改我的代码,但如果我这样做,我的应用程序将会崩溃! (删除并再次构建应用程序不是一个选项.. 正如我提到的,它已经有了它的数据结构)

NSPersistentStoreCoordinator中我使用了以下代码:

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

现在我必须使用它:

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

如您所见,我必须将 ofType 从 NSSQLiteStoreType 更改为 SMStore.type

有什么办法吗?有人知道吗?我希望有人能提供帮助。

非常感谢。

您可以为此使用 NSPersistentStoreCoordinator 方法 migratePersistentStore(_:to:options:withType:)。此方法将现有持久存储文件移动到新文件,并且可以更改持久存储类型。虽然名字里有"migrate",但跟模型版本迁移没有任何关系。在这种情况下,它将持久存储迁移到新文件。

您必须使用旧存储类型添加持久存储,然后迁移到新存储类型。