6

如果我想为给定的实体使用自定义迁移策略,我相信我必须在类名称前加上产品模块名称,如下图所示:

映射模型检查器

我怎样才能设法处理多个目标?

我尝试使用以下条目:$(PRODUCT_MODULE_NAME).VisitToVisitPolicy但这似乎不起作用。我仍然有可能复制映射模型,每个目标一个,但感觉不对。

4

2 回答 2

2

尝试在应用程序和测试目标之间共享模型文件时遇到同样的问题。几乎放弃并认为我将不得不使用您的重复黑客,但幸运的是找到了一个理智的方法:

// 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)
于 2018-10-19T08:32:05.807 回答
0

我有同样的问题。我的解决方案类似于 Alexander 的解决方案,它应该适用于多个迁移策略(每个实体一个)。您需要设置Custom Policy为没有任何命名空间的类名,并且在获得映射模型后我这样做:

    mapping.entityMappings.forEach {
        if let entityMigrationPolicyClassName = $0.entityMigrationPolicyClassName,
            let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String {
            $0.entityMigrationPolicyClassName = "\(namespace).\(entityMigrationPolicyClassName)"
        }
    }
于 2019-09-25T10:10:19.053 回答