“自动轻量级迁移”的实现核心数据(iPhone) [英] Implementation of "Automatic Lightweight Migration" for Core Data (iPhone)
问题描述
当我向我的核心数据模型添加
新属性时,我想让我的应用程序能够进行自动轻量级迁移。
从苹果这是唯一的信息,我可以找到的主题:
自动轻量级迁移
要请求自动轻量级
迁移,请在传递的选项字典中设置适当的标志
addPersistentStoreWithType:configuration:URL:options:error: 。
您需要将对应
的值设置为
NSMigratePersistentStoresAutomaticallyOption
和
NSInferMappingModelAutomaticallyOption
键为YES:
NSError * error;
NSURL * storeURL =<#持久性存储的URL#> ;;
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]){
//处理错误。
}
我的 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;
}
我无法看到我应该如何添加苹果代码以获得自动轻量级迁移工作?
这是我做了自动轻量级迁移: http://brainwashinc.wordpress.com/ 2010/01/18 / iphone-coredata-automatic-light-migration / )
1。
将您的persistentStoreCoordinator创建更改为此(替换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]){
//处理错误
}
return persistentStoreCoordinator;
}
2。
选择您的xcdatamodel文件
设计 - >数据模型 - >添加模型版本(展开您的xcdatamodeld项目)
选择2(或更高版本)文件,设计 - >数据模型 - >设置当前版本(编辑此版本)
3。在应用程式委托中指定momd资源。
将您的managedObjectModel实作变更为这个(替换YOURDB)
- (NSManagedObjectModel *)managedObjectModel {
if(managedObjectModel!= nil){
return managedObjectModel;
}
NSString * path = [[NSBundle mainBundle] pathForResource:@YOURDBofType:@momd];
NSURL * momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return managedObjectModel;
}
I would like to make my app able to do an automatic lightweight migration when I add new attributes to my core data model.
In the guide from Apple this is the only info on the subject I could find:
Automatic Lightweight Migration
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.
}
My NSPersistentStoreCoordinator
is initialized in this way:
- (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;
}
I am having trouble seeing where and how I should add the Apple code to get the Automatic Lightweight Migration working?
This is what I did to make Automatic Lightweight Migration (Source: http://brainwashinc.wordpress.com/2010/01/18/iphone-coredata-automatic-light-migration/)
1. Set the Persistent Store options for automatic migration in the app delegate.
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. 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)
3. Specify the momd resource in app delegate.
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屋!