Selonath
Selonath

Reputation: 23

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! (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:

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

Is there any way to do that? Is anyone has any idea? I hope someone could help.

Thank you very much.

Upvotes: 2

Views: 832

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70956

You can use the NSPersistentStoreCoordinator method migratePersistentStore(_:to:options:withType:) for this. 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.

Upvotes: 1

Related Questions