“自动轻量级迁移"的实现用于核心数据 (iPhone) [英] Implementation of "Automatic Lightweight Migration" for Core Data (iPhone)
问题描述
我想让我的应用程序能够在添加时自动进行轻量级迁移我的核心数据模型的新属性.
I would like to make my app able to do an automatic lightweight migration when I add new attributes to my core data model.
在 Apple 的指南中,这是我能找到的有关该主题的唯一信息:
In the guide from Apple this is the only info on the subject I could find:
自动轻量级迁移
请求自动轻量级迁移,您设置适当的标志在您传入的选项字典中addPersistentStoreWithType:configuration:URL:options:error:.您需要设置相应的值对双方NSMigratePersistentStoresAutomaticallyOption和NSInferMappingModelAutomaticallyOption是的关键:
To request automatic lightweight migration, you set appropriate flags in the options dictionary you pass in addPersistentStoreWithType:configuration:URL:options:error:. You need to set values corresponding to both the NSMigratePersistentStoresAutomaticallyOption and the NSInferMappingModelAutomaticallyOption keys to YES:
NSError *error;
NSURL *storeURL = <#The URL of a persistent store#>;
NSPersistentStoreCoordinator *psc = <#The coordinator#>;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![psc addPersistentStoreWithType:<#Store type#>
configuration:<#Configuration or nil#> URL:storeURL
options:options error:&error]) {
// Handle the error.
}
我的NSPersistentStoreCoordinator
是这样初始化的:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"FC.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
我无法查看应该在何处以及如何添加 Apple 代码以使自动轻量级迁移工作?
推荐答案
这就是我为实现自动轻量级迁移所做的工作(来源:http://brainwashinc.wordpress.com/2010/01/18/iphone-coredata-automatic-light-migration/)
This is what I did to make Automatic Lightweight Migration (Source: http://brainwashinc.wordpress.com/2010/01/18/iphone-coredata-automatic-light-migration/)
将您的 persistentStoreCoordinator 创建更改为此(替换 YOURDB):
Change your persistentStoreCoordinator creation to this (replace YOURDB):
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"YOURDB.sqlite"]];
// handle db upgrade
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Handle error
}
return persistentStoreCoordinator;
}
2.版本您的数据模型并编辑新文件.
选择您的 xcdatamodel 文件设计 -> 数据模型 -> 添加模型版本(展开你的 xcdatamodeld 项目)选择2"(或更高版本)文件,Design -> Data Model -> Set Current Version(编辑此版本)
2. Version your Data Model and Edit the new file.
Select your xcdatamodel file Design -> Data Model -> Add Model Version (expand your xcdatamodeld item) Select the "2″ (or later) file, Design -> Data Model -> Set Current Version (edit this version)
将您的 managedObjectModel 实现更改为此(替换 YOURDB)
Change your managedObjectModel implementation to this (replace YOURDB)
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"YOURDB" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return managedObjectModel;
}
这篇关于“自动轻量级迁移"的实现用于核心数据 (iPhone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!