Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 具有多个目标的核心数据自定义迁移策略_Ios_Core Data_Mapping Model - Fatal编程技术网

Ios 具有多个目标的核心数据自定义迁移策略

Ios 具有多个目标的核心数据自定义迁移策略,ios,core-data,mapping-model,Ios,Core Data,Mapping Model,如果我想对给定实体使用自定义迁移策略,我相信我必须以产品模块名称作为类名的前缀,如下图所示: 如何处理多个目标? 我尝试使用以下条目:$(产品\模块\名称)。VisitToVisitPolicy,但这似乎不起作用。我仍然有可能复制映射模型,每个目标一个,但这感觉不太对。在应用程序和测试目标之间共享模型文件时也遇到同样的问题。几乎放弃了,我想我将不得不使用你的复制黑客,但谢天谢地,我找到了一个理智的方法: // Get mapping model let mappingModel = NSMap

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

如何处理多个目标?


我尝试使用以下条目:
$(产品\模块\名称)。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)"
        }
    }