Search code examples
xcodecore-datacore-data-migration

Core Data Migration custom migration


I'm trying to do a custom migration, following the instructions found here, changing an attribute name and type but it doesn't seem to be running the mapping.

Old model has an attribute names "roundDrill" which is a Boolean. The new model has this removed and a new attribute called "drillType" which is a String.

I have a mapping model called "V2V3.xcmappingmodel" where the entity has a custom policy set "Diamond_Painting_Logbok.V2V3". The new attribute has a value of "FUNCTION($entityPolicy, "roundToType:" , $source.roundDrill)"

In the "V2V3.swift" I have the below code

import UIKit
import CoreData

class V2V3: NSEntityMigrationPolicy {
    func roundToType(isRound:NSNumber) -> String {
        NSLog("Round: \(isRound)")
        if isRound.boolValue {
            return "Round"
        } else {
            return "Square"
        }
    }
}

When I print out the value of drillType, I'm getting nil. The NSLog on the function isn't being printed at all. If I change the custom policy name, its not giving me an error so appears as though its being ignored completely


Solution

  • So I've found the reason for the issue after speaking to apple, its because I don't have a V1 to V2 model, just V2 to V3. Without that, all mapping models are ignored,

    They've also said the mapping isn't running correctly as it crashes with "unrecognized selector sent to instance" even though ititss correct, that its a bug.

    They've advised to use createDestinationInstances as a workaround so need to figure that out