这是基本过程:
创建数据模型的版本化副本。 (选择模型,然后编辑器->添加模型版本)
对数据模型的新副本进行更改
将新数据模型的副本标记为当前版本。 (单击顶级 xcdatamodel 项,然后在文件检查器中将“版本化数据模型”部分下的“当前”条目设置为您在步骤 1 中创建的新数据模型。
更新模型对象以添加 RecipeIngredient 实体。还要将配方和成分实体上的成分和配方关系替换为您在步骤 2 中创建的与 RecipeIngredient 实体的新关系。 (两个实体都添加了这个关系。我称之为我的recipeIngredients)显然,无论您在旧代码中创建从成分到配方的关系,您现在都需要创建一个RecipeIngredient对象..但这超出了这个答案的范围。
在模型之间添加新的映射(文件->新文件...->(核心数据部分)->映射模型。这将为您自动生成多个映射。RecipeToRecipe、IngredientToIngredient 和 RecipeIngredient。
删除食谱成分映射。还要删除它为 RecipeToRecipe 和 IngredientToRecipe (或您在步骤 2 中调用的任何名称)提供的 RecipeIngredient 关系映射。
将 RecipeToRecipe 映射拖到映射规则列表的最后。 (这是重要的这样我们就可以确保成分在食谱之前迁移,以便我们在迁移食谱时可以将它们链接起来。)迁移将按照迁移规则列表的顺序进行。
为 RecipeToRecipe 映射“DDCDRecipeMigrationPolicy”设置自定义策略(这将覆盖 Recipes 对象的自动迁移,并为我们提供一个可以执行映射逻辑的钩子。
通过子类化食谱的 NSEntityMigrationPolicy 来覆盖 createDestinationInstancesForSourceInstance 来创建 DDCDRecipeMigrationPolicy(请参阅下面的代码)。这将为每个食谱调用一次,这将让我们创建食谱对象,以及将其链接到成分的相关食谱成分对象。我们只需让 Ingredient 通过 Xcode 在步骤 5 中自动创建的映射规则自动迁移即可。
无论您在何处创建持久对象存储(可能是 AppDelegate),请确保设置用户字典以自动迁移数据模型:
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, nil]
error:&error])
{
}
食谱的子类 NSEntityMigrationPolicy
#import <CoreData/CoreData.h>
@interface DDCDRecipeMigrationPolicy : NSEntityMigrationPolicy
@end
*覆盖 DDCDRecipeMigrationPolicy.m 中的 createDestinationInstancesForSourceInstance *
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
NSLog(@"createDestinationInstancesForSourceInstance : %@", sInstance.entity.name);
//We have to create the recipe since we overrode this method.
//It's called once for each Recipe.
NSManagedObject *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:[manager destinationContext]];
[newRecipe setValue:[sInstance valueForKey:@"name"] forKey:@"name"];
[newRecipe setValue:[sInstance valueForKey:@"overview"] forKey:@"overview"];
[newRecipe setValue:[sInstance valueForKey:@"instructions"] forKey:@"instructions"];
for (NSManagedObject *oldIngredient in (NSSet *) [sInstance valueForKey:@"ingredients"])
{
NSFetchRequest *fetchByIngredientName = [NSFetchRequest fetchRequestWithEntityName:@"Ingredient"];
fetchByIngredientName.predicate = [NSPredicate predicateWithFormat:@"name = %@",[oldIngredient valueForKey:@"name"]];
//Find the Ingredient in the new Datamodel. NOTE!!! This only works if this is the second entity migrated.
NSArray *newIngredientArray = [[manager destinationContext] executeFetchRequest:fetchByIngredientName error:error];
if (newIngredientArray.count == 1)
{
//Create an intersection record.
NSManagedObject *newIngredient = [newIngredientArray objectAtIndex:0];
NSManagedObject *newRecipeIngredient = [NSEntityDescription insertNewObjectForEntityForName:@"RecipeIngredient" inManagedObjectContext:[manager destinationContext]];
[newRecipeIngredient setValue:newIngredient forKey:@"ingredient"];
[newRecipeIngredient setValue:newRecipe forKey:@"recipe"];
NSLog(@"Adding migrated Ingredient : %@ to New Recipe %@", [newIngredient valueForKey:@"name"], [newRecipe valueForKey:@"name"]);
}
}
return YES;
}
我会发布 Xcode 中的设置图片和示例 Xcode 项目,但我似乎在堆栈溢出上没有任何声誉点......所以它不会让我这样做。我也会将其发布到我的博客上。 bingosabi.wordpress.com/。
另请注意,Xcode 核心数据模型映射内容有点不稳定,有时需要“干净”、良好的 Xcode 恢复器、模拟器弹跳或以上所有功能才能使其正常工作。