iphone - 核心数据 (iPhone) 的 "Automatic Lightweight Migration"实现

标签 iphone core-data data-migration

我想让我的应用程序能够在添加时自动进行轻量级迁移 我的核心数据模型的新属性。

在 Apple 的指南中,这是我能找到的有关该主题的唯一信息:

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.
}

我的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/)

1。在应用委托(delegate)中设置用于自动迁移的持久存储选项。

将您的 persistenceStoreCoordinator 创建更改为此(替换 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(编辑此版本)

3。在应用程序委托(delegate)中指定 momd 资源。

将你的 ManagedObjectModel 实现更改为此(替换 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 - 核心数据 (iPhone) 的 "Automatic Lightweight Migration"实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2310216/

相关文章:

ios - 加载主 TableViewController 后值从 CoRe 数据中消失

ios - 为什么XML存储在iOS中不可用?

objective-c - 将 DBAccess 转换为 swift

cqrs - 将关系数据迁移到事件存储

sql - 我应该如何将这些数据迁移到这些 Sql Server 表中?

ios - 什么是恢复标识符?

iPhone - 无法被杀死的应用程序

iPhone,如何在静音或静音模式下播放声音?

django - 南数据从父类迁移到子类clobbers父数据

iphone - UiimagePIckerController 与相机和视频接口(interface)