Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data custom migration policy with multiple targets

If I want to use a custom migration policy for a given entity, I believe I have to prefix the class name by the product module name, as shown on the following image:

Mapping Model Inspector

How can I manage to handle multiple targets?

I tried using the following entry: $(PRODUCT_MODULE_NAME).VisitToVisitPolicy but this does not seem to work. I still have the possibility to duplicate the mapping model, one for each target, but that does not feel right.

like image 562
adauguet Avatar asked Jan 16 '18 15:01

adauguet


1 Answers

Had the same problem trying to share model files between app and test targets. Almost gave up and thought I'll have to use your duplicate hack, but thankfully found a sane way:

// Get mapping model
let mappingModel = NSMappingModel(from: [.main], 
                        forSourceModel: sourceModel, 
                      destinationModel: destinationModel)!

// Get migration policy class name that also includes the module name
let fullClassName = NSStringFromClass(NSEntityMigrationPolicySubclass.self)

// Set policy here (I have one policy per migration, so this works)
mappingModel.entityMappings.forEach { 
    $0.entityMigrationPolicyClassName = fullClassName 
}

// Migrate
let manager = NSMigrationManager(sourceModel: sourceModel, 
                            destinationModel: destinationModel)

try! manager.migrateStore(from: sourceURL, 
                    sourceType: NSSQLiteStoreType, 
                       options: nil, 
                          with: mappingModel, 
              toDestinationURL: destinationURL, 
               destinationType: NSSQLiteStoreType, 
            destinationOptions: nil)
like image 162
Alexander Borisenko Avatar answered Nov 20 '22 10:11

Alexander Borisenko