在Core Data中进行大量重量迁移之后试图保存数据

我正在处理需要大量重量迁移的iOS应用程序。 我正在做的是将我旧的数据模型中Integer64types的实体的属性types转换为新数据模型中的stringtypes。 因为我正在改变属性的types,这需要大量的重量移植。

现在,转换工作正常,但不幸的是,我在转换后保存新实体时遇到了问题,这就是为什么当我在迁移后启动我的应用程序时,我无法查看使用旧的数据模型。 这里是我使用的NSEntityMigrationPolicy的子类:

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError *__autoreleasing *)error { NSManagedObject *newObject; NSEntityDescription *sourceInstanceEntity = [sInstance entity]; NSManagedObjectContext *destMOC = [manager destinationContext]; //correct entity? just to be sure if ([[sourceInstanceEntity name] isEqualToString:@"MyEntity"]) { newObject = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:destMOC]; //obtain the attributes NSDictionary *keyValDict = [sInstance committedValuesForKeys:nil]; NSDictionary *allAttributes = [[sInstance entity] attributesByName]; //loop over the attributes for (NSString *key in allAttributes) { //get key and value id value = [sInstance valueForKey:key]; if ([key isEqualToString:@"myAttribute"]) { //here retrieve old value NSNumber *oldValue = [keyValDict objectForKey:key]; //here do conversion as needed NSString *newValue = [oldValue stringValue]; //then store new value [newObject setValue:newValue forKey:key]; } else { //no need to modify the value, Copy it across [newObject setValue:value forKey:key]; } } [manager associateSourceInstance:sInstance withDestinationInstance:newObject forEntityMapping:mapping]; [destMOC save:error]; } return YES; } - (BOOL) createRelationshipsForDestinationInstance:(NSManagedObject *)dInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError *__autoreleasing *)error { return YES; } 

我试图尽可能地彻底,而且我也join了迁移过程,但不幸的是,我不知道为什么我要转换的实体没有被保存在新的数据模型中。 我想指出一些可能的原因:

我正在转换/迁移的这个实体与其他4个实体有4个一对一的关系:一个关系有一个相反的关系,其中三个关系没有相反的关系。 我知道build立一个没有逆的关系是不被推荐的,但是这是原始数据模型的devise原理,不幸的是我无能为力。 但是,这些关系不会以任何方式从旧数据模型更改为新数据模型。 我的方法是:

 - (BOOL) createRelationshipsForDestinationInstance:(NSManagedObject *)dInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError *__autoreleasing *)error { return YES; } 

现在必须改变,以适应这种情况,从而允许我保存我的数据,或者我可以单独离开这个方法,并保存数据,我只需要改变方法:

 - (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError *__autoreleasing *)error {...} 

并保持现有的关系,因为它们是完整的?

我通过将所有的关系types改变为在原始模型中具有相反的关系来解决这个问题,并且在新的模型中保持相同的结构。 当我使用我的上述代码时,一切正常。

感谢所有考虑过这个问题的人。