我正在进行手动迁移,主要使用此 stackoverflow 答案作为指南: https ://stackoverflow.com/a/8155531/5416
我有 3 个单独的实体需要迁移。每个属性只有一个在变化,它从整数变为字符串。一些实体似乎运行良好,没有抛出异常,但该过程没有完成。它列出了一堆本质上完全相同的错误:
错误域 = NSCocoaErrorDomain 代码 = 1570 \"操作无法\U2019t 完成。(Cocoa 错误 1570。)\" UserInfo=0x2a4c2790 {NSValidationErrorObject=NSManagedObject_CCRecipeIngredient_2:, NSValidationErrorKey=name, NSLocalizedDescription=操作无法\U2019t 完成。(可可错误 1570。)}
任何想法如何最好地解决这个问题?如果有帮助,这是我正在使用的迁移策略:
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)aSource
entityMapping:(NSEntityMapping *)mapping
manager:(NSMigrationManager *)migrationManager
error:(NSError **)error {
NSString *attributeName = @"foodId";
NSEntityDescription *aSourceEntityDescription = [aSource entity];
NSString *aSourceName = [aSourceEntityDescription valueForKey:@"name"];
NSManagedObjectContext *destinationMOC = [migrationManager destinationContext];
NSManagedObject *destEntity;
NSString *destEntityName = [mapping destinationEntityName];
if ([aSourceName isEqualToString:@"CCFood"] || [aSourceName isEqualToString:@"CCFoodLogEntry"] || [aSourceName isEqualToString:@"CCRecipeIngredient"] )
{
destEntity = [NSEntityDescription
insertNewObjectForEntityForName:destEntityName
inManagedObjectContext:destinationMOC];
// attribute foodid
NSNumber *sourceFoodID = [aSource valueForKey:attributeName];
if (!sourceFoodID)
{
[destEntity setValue:@"0" forKey:attributeName];
}
else
{
NSInteger sourceFoodIDInteger = [sourceFoodID intValue];
NSString *sourceFoodIDString = [NSString stringWithFormat:@"%i", sourceFoodIDInteger];
[destEntity setValue:sourceFoodIDString forKey:attributeName];
}
[migrationManager associateSourceInstance:aSource
withDestinationInstance:destEntity
forEntityMapping:mapping];
return YES;
} else
{
// don't remap any other entities
return NO;
}
}