我正在进行手动迁移,主要使用这个 stackoverflow 答案作为指南: https://stackoverflow.com/a/8155531/5416
我有 3 个独立的实体需要迁移。每个上只有一个属性发生变化,并且它从整数变为字符串。一些实体似乎一切顺利,没有抛出异常,但该过程尚未完成。它列出了一堆本质上完全相同的错误:
Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo=0x2a4c2790 {NSValidationErrorObject=NSManagedObject_CCRecipeIngredient_2:, NSValidationErrorKey=name, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 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;
}
}
最佳答案
好吧,我想这真的只是我误解了这个 API 的工作原理。对象的所有属性都不会自动映射。我当时(错误地)假设我只需要设置我正在映射的属性。
最后,我所要做的就是遍历源实体的属性并将它们分配给目标实体。
- (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];
// migrate all attributes
NSEntityDescription *entity = [aSource entity];
NSDictionary *attributes = [entity attributesByName];
for (NSString *attribute in attributes) {
if ([attribute isEqualToString:@"foodId"]) {
// migrate the food id
NSNumber *sourceFoodID = [aSource valueForKey:attributeName];
if (!sourceFoodID)
{
[destEntity setValue:@"0" forKey:attributeName];
NSLog(@"migrating %@: empty foodid", aSourceName);
}
else
{
NSInteger sourceFoodIDInteger = [sourceFoodID intValue];
NSString *sourceFoodIDString = [NSString stringWithFormat:@"%i", sourceFoodIDInteger];
[destEntity setValue:sourceFoodIDString forKey:attributeName];
NSLog(@"migrating %@ # %@", aSourceName, sourceFoodIDString);
}
}
else {
// not the foodid, so just pass it along
id value = [aSource valueForKey: attribute];
[destEntity setValue:value forKey:attribute];
}
}
[migrationManager associateSourceInstance:aSource
withDestinationInstance:destEntity
forEntityMapping:mapping];
return YES;
} else
{
// don't remap any other entities
return NO;
}
}
关于ios - 无用的核心数据迁移错误 - 操作无法完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12923776/