I've been testing out some migrations using code from @Fat Xu's blog (thank you for that!), and I've run into the "Duplicate version checksums across stages detected" error a few times. By using only NSCustomMigrationStage and not NSLightweightMigrationStage, it seems to work.
My observations start with the following setup:
My Model has four versions: v1, v2, v3, v4.
V1ToV2 and V3ToV4 can use lightweight migrations.
V2ToV3 requires a custom stage
What I found was that if I created the NSStagedMigrationManager like this:
let myCustomMigrationStageV2toV3 = NSCustomMigrationStage(
migratingFrom: v2,
to: v3)
myCustomMigrationStageV2toV3.willMigrateHandler = /* do custom migration stuff */
let migrationManager = NSStagedMigrationManager([
NSLightweightMigrationStage([v1.checksum]),
myCustomMigrationStageV2toV3,
NSLightweightMigrationStage([v3.checksum])
])
I would get the "Duplicate version checksums" error, probably because the custom stage and the third stage both included v3. If I change the final stage to v4.checksum, the migration succeeds. I find this somewhat confusing because in two NSLightweightMigrationStages with the same initializer, the provided version checksum is the source version in one, and the destination version in the other.
A more reliable option for me was to use NSCustomMigrationStage even where NSLightweightMigrationStage would work. For example...
let migrationManager = NSStagedMigrationManager([
NSCustomMigrationStage(migratingFrom: v1, to: v2),
myCustomMigrationStageV2toV3,
NSCustomMigrationStage(migratingFrom: v3 to: v4)
])
This works fine, and I get to be certain of each stage along the way having the source and destination versions that I specify. It seems a NSCustomMigrationStage with no willMigrateHandler and no didMigrateHandler is just the same as a NSLightweightMigrationStage, but with an initializer that makes more sense.
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: