阅读您的第一句话,我假设您的“CoreDataModel”和“ModuleDataModel”完全分离,即(A 或 B)与(C 或 D)之间没有关系。
我可能会尝试使用一个或多个 NSEntityMigrationPolicy 和指定的 NSMigrationManager 执行迁移,使用(代码片段):
NSMigrationManager *migrationManager=[[NSMigrationManager alloc] initWithSourceModel: sourceModel destinationModel: destModel];
BOOL success=[migrationManager migrateStoreFromURL: sourceStoreURL type: NSSQLiteStoreType options: nil withMappingModel: mappingModel toDestinationURL: destinationStoreURL destinationType: NSSQLiteStoreType destinationOptions: nil error: &migrationError];
您的迁移政策可以覆盖:
- (BOOL) createDestinationInstancesForSourceInstance: (NSManagedObject*) instance entityMapping: (NSEntityMapping*) mapping manager: (NSMigrationManager*) manager error: (NSError* __autoreleasing *) error
在此期间,您可以将相关(C、D)源实体的属性(和 entityType)存储在字典中,将其添加到数组并将该数组存储在 -[NSMigrationManager userInfo]字典。在这个阶段,您实际上只会将 A、B 实体迁移到您的新商店。
一旦初始迁移完成(通常在设置您的 main /“CoreData”persistentStoreCoordinator 时),您可以检索 migrationManager 的 userInfo 字典并存储您的字典数组,例如在您的应用委托/核心数据控制器的属性中。
NSManagedContexts 初始化后,“CoreData”应该被正确迁移,但“ModuleData”将为空。然后,您可以遍历存储的数组,创建 C 和 D 的新实体并设置它们的属性。
我没有介绍 C 和 D 关系的额外复杂性,但希望这将是一个正确方向的指针。当我想将一个标准的 MOM/PSC/MOC 分成一个具有两个磁盘存储的 MOM/MOC 时,我做了类似的事情,由同一个 PSC 管理。它工作得很好,虽然看起来有点不整洁,但我无法单独使用迁移策略来解决。如果有更好的方法,我会很感兴趣。