我正在手工遷移,主要使用這個計算器的答案作爲指導: https://stackoverflow.com/a/8155531/5416無益核心數據遷移錯誤 - 該操作無法完成
我有需要遷移的3個獨立的實體。每個屬性只有一個屬性發生變化,並且從一個整數變爲一個字符串。一些實體似乎經歷得很好,並沒有拋出任何例外,但這個過程並沒有完成。它列出了一堆是錯誤的所有的基本如出一轍:
錯誤域= NSCocoaErrorDomain代碼= 1570 \「操作 couldn \ U2019t完成(可可錯誤1570)\」的UserInfo = 0x2a4c2790 {NSValidationErrorObject = NSManagedObject_CCRecipeIngredient_2:, NSValidationErrorKey = name,NSLocalizedDescription =操作 無法完成。 (可可錯誤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;
}
}
感謝您的回覆,這對我有很大幫助 – user2378197