NSStagedMigrationManager Merging Steps

Hello,

I have 3 model versions and I'm trying to step through migration.

Version 2 makes significant changes to v1. As a result, I've renamed the entities in question by appending _v2 to their name, as the data isn't important to retain.

v3, remove's the appended version number from v2.

Setting the .xcdatamodeld to v3 and the migrations steps array as follows causes the app to error

[
                NSLightweightMigrationStage([v1]),
                NSLightweightMigrationStage([v2]),
                NSLightweightMigrationStage([v3]),
]
CoreData: error: <NSPersistentStoreCoordinator: 0x10740d680>: Attempting recovery from error encountered during addPersistentStore: 0x10770f8a0 Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." 

An error occurred during persistent store migration. Cannot merge multiple root entity source tables into one destination entity root table.

I find this odd because if I run the migration independently across app launches, the migration appears to drop the no longer used tables in v2, then re-add them back in v3. So it seems to me that something is not finishing completely with the fully stepped through migration.

--

I'm also unable to understand how to use NSCustomMigrationStage I've tried setting it to migrate from v1, to v2, but I'm getting a crash with error

Duplicate version checksums across stages detected

There was a few issues here. None of which are documented very well. And all crash with the same error message.

(1) My orginal .xcdatamodel file didn't have a Core Data Model Identifier. Which lead to a crash with NSCustomMigrationStage, even though the versionChecksum are different.

(2) The steps passed into NSStagedMigrationManager are not clear or documented. But it appears that if you pass the first version of the model into the steps, it crashes.

(3) I'm using manual code generation for the model classes. So my goal was to switch the model names, then switch them back, so I didn't have to change anything.

For some reason, stepping through the one migration step per app launch instead of all at once allowed for a successful migration. But running all steps threw an error.

Turns out I could just rename the model classes with the _v2 suffix, and the only thing I needed to update was the string in the fetch request.

Overall, to resolve this

  • I added a model version identifier
  • I duplicated the model version and incremented the model version identifier
  • Deleted the final migration step that removed the entity versioning
NSStagedMigrationManager Merging Steps
 
 
Q