具有多个目标的 Core Data 自定义迁移策略

ada*_*uet 5 core-data ios mapping-model

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

映射模型检查器

如何处理多个目标?

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

Ale*_*nko 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)
Run Code Online (Sandbox Code Playgroud)