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