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)