如果我想为给定实体使用自定义迁移策略,我相信我必须在产品模块名称前加上类名称,如下图所示:
如何设法处理多个目标?
我尝试使用以下条目:$(PRODUCT_MODULE_NAME).VisitToVisitPolicy
但这似乎不起作用。我仍然有可能复制映射模型,每个目标一个,但感觉不对。
答案 0 :(得分:0)
尝试在应用程序和测试目标之间共享模型文件时遇到相同的问题。几乎放弃了,以为我将不得不使用重复的hack,但幸运的是找到了一种理智的方法:
// 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)
答案 1 :(得分:0)
我有同样的问题。我的解决方案类似于Alexander's的解决方案,它应适用于多种迁移策略(每个实体一个)。您需要将Custom Policy
设置为没有任何名称空间的类名,并且在获取映射模型后执行以下操作:
mapping.entityMappings.forEach {
if let entityMigrationPolicyClassName = $0.entityMigrationPolicyClassName,
let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String {
$0.entityMigrationPolicyClassName = "\(namespace).\(entityMigrationPolicyClassName)"
}
}