我有一个 iPhone 应用程序,它使用多对多关系将标签和注释链接在一起。我目前正在使用 Core Data 的“关系”功能来完成此操作,但我想改用连接表。
这是我的挑战:我想从旧模型迁移到连接表模型,我需要弄清楚如何执行该数据迁移。
有什么好的例子可以说明如何做到这一点吗?
更新:我在这里澄清我的问题以帮助解决这里发生的事情:我想尝试使用 Simperium支持我们的应用程序,但 Simperium 不支持多对多关系(!)。
作为我正在尝试做的事情的示例,让我们以 iPhoneCoreDataRecipes 应用为例。
这是我的 Core Data 方案目前的样子:
...这就是我要过渡到的内容:
我如何从一个到另一个,并随身携带数据?
Apple 的核心数据迁移文档是出了名的稀疏,而且我没有看到任何使用 NSEntityMapping 或 NSMigrationManager 子类来完成工作的有用演练。
最佳答案
基本流程:
创建数据模型的版本化副本。 (选择Model,然后Editor->Add Model Version)
对数据模型的新副本进行更改
将新数据模型的副本标记为当前版本。 (单击顶级 xcdatamodel 项,然后在文件检查器中将“版本化数据模型”部分下的“当前”条目设置为您在步骤 1 中创建的新数据模型。
更新您的模型对象以添加 RecipeIngredient 实体。此外,将 Recipe 和 Ingredient 实体上的成分和食谱关系替换为您在步骤 2 中创建的与 RecipeIngredient 实体的新关系。 (两个实体都添加了这种关系。我称之为 recipeIngredients)显然,无论您在旧代码中创建从成分到配方的关系,您现在都需要创建一个 RecipeIngredient 对象..但这超出了这个答案的范围。
在模型之间添加一个新映射(文件->新文件...->(核心数据部分)->映射模型。这将为您自动生成多个映射。RecipeToRecipe、IngredientToIngredient 和 RecipeIngredient。
删除 RecipeIngredient 映射。同时删除它为 RecipeToRecipe 和 IngredientToRecipe(或您在第 2 步中调用的任何名称)提供的 recipeIngredient 关系映射。
将 RecipeToRecipe 映射拖到映射规则列表的最后。 (这很重要,这样我们可以确保成分在食谱之前迁移,这样我们就可以在迁移食谱时将它们链接起来。)迁移将按照迁移规则列表的顺序进行.
为 RecipeToRecipe 映射“DDCDRecipeMigrationPolicy”设置自定义策略(这将覆盖 Recipes 对象的自动迁移并为我们提供一个可以执行映射逻辑的 Hook 。
通过为 Recipes 子类化 NSEntityMigrationPolicy 来创建 DCDCDRecipeMigrationPolicy,以覆盖 createDestinationInstancesForSourceInstance(参见下面的代码)。这将为每个 Recipe 调用一次,这将使我们创建 Recipe 对象,以及将其链接到 Ingredient 的相关 RecipeIngredient 对象。我们将让 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
*覆盖 DCDCDRecipeMigrationPolicy.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 恢复器、模拟器反弹或以上所有方法才能使其正常工作。
关于iphone - 将多对多关系迁移到 Core Data 中的连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11174773/