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

时间:2017-01-16 10:38:19

标签: ios swift sqlite core-data cloudkit

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

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

有没有办法做到这一点?有人有什么想法吗?我希望有人可以提供帮助。

非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用NSPersistentStoreCoordinator方法migratePersistentStore(_:to:options:withType:)。此方法将现有持久性存储文件移动到新文件,并可以更改持久性存储类型。即使它在名称中“迁移”,它也与模型版本迁移无关。在这种情况下,它将持久存储迁移到新文件。

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

相关问题