iphone - Coredata手动迁移

标签 iphone cocoa-touch core-data ios4 migration

我正在 iOS 项目中使用核心数据。每当我修改实体属性时,我都会设置自动数据迁移的数据模型。但我最近对一些实体关系进行了一些更改,现在我的应用程序崩溃并显示:“找不到源存储的模型”

我意识到重置应用程序,即删除并重新安装将解决此问题,但我已经有了实时版本,我的用户将丢失所有数据!

所以现在我正在尝试手动迁移,但 iOS 文档不是很有帮助。例如,我有以下代码,在创建模型映射后运行:

NSURL *destinationStoreURL = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"import.sqlite"]];

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"db.sqlite"]]; 

//initialize migration manager
NSMigrationManager *migrationManager = [[NSMigrationManager alloc] initWithSourceModel:[[self persistentStoreCoordinator] managedObjectModel]
                                                                      destinationModel:[[self persistentStoreCoordinator] managedObjectModel]];

//perform migration     
NSError *error = nil;       
NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:[[self persistentStoreCoordinator] managedObjectModel]
                                                                 destinationModel:[[self persistentStoreCoordinator] managedObjectModel] error:&error];

if (mappingModel == nil) {
    NSLog(@"No Mapping model error %@, %@", error, [error userInfo]);
}

[migrationManager migrateStoreFromURL:sourceStoreURL
                                 type:NSSQLiteStoreType
                              options:nil
                     withMappingModel:mappingModel
                     toDestinationURL:destinationStoreURL
                      destinationType:NSSQLiteStoreType
                   destinationOptions:nil
                                error:&error];  

运行此代码可以工作并重置数据库,但我找不到旧数据,并且当我保存任何新数据时,我收到一条错误,指出没有持久存储!

有人有什么想法吗?

最佳答案

这些人是对的......

如果还不算太晚,请尝试以下操作:打开“[您的数据库].xcdatamodel”文件。然后(假设您使用的是 Xcode),转到主菜单。选择设计 > 数据模型 > 添加模型版本。这将创建一个新文件,在我们的例子中为“[您的数据库] 2.xcdatamodel”:

现在转到“设计”>“数据模型”>“设置当前版本”。这样做会告诉 Xcode 这是您将使用的数据库架构。现在进行您想要的任何数据库架构更改。现在可能是对直接影响数据库架构更改的任何代码进行更改的好时机。

现在编译你的程序。这次应该加载了。

我也遇到了同样的麻烦。这是我第一次真正阅读 iPhone 开发文档。我必须非常注意。现在我已经准备好了。我其实选择了轻量级迁移。这段代码是直接从一个(或多个)Apple 示例程序中劫持的(这些程序通常有错误,只是为了让你知道......:-/)

 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 
 if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
 }

 NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
      stringByAppendingPathComponent:DATABASENAME]];
 NSError *error;
 persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:      
  [self managedObjectModel]];

 // Allow inferred migration from the original version of the application.
 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], 
        NSMigratePersistentStoresAutomaticallyOption,
        [NSNumber numberWithBool:YES], 
         NSInferMappingModelAutomaticallyOption, nil];

 //ATTENTION: YOU WERE CRASHING HERE...
 if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
 configuration:nil URL:storeUrl options:options error:&error]) {
       // Handle the error.
      NSLog(@"WTF??? FAILED to create automatic lightweight migration. Error[%@]", error);
 }    

关于iphone - Coredata手动迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3736111/

相关文章:

ios - 来自NSLog的Segfault?

iphone - 显示 CGRect 位置重置我的图像位置

iphone - 如何在一次操作中从 UINavigationController 中弹出一个 View 并将其替换为另一个 View ?

iphone - 当用户点击“编辑”按钮时重做 UITableView 布局?

objective-c - 核心数据-带日期的谓词

ios - CloudKit 如何与多个设备同步?

iphone - 无需拉起键盘即可在 iOS 5.1 上获取听写输入

iphone - 如何在iPhone中获得最接近的更大整数值

iphone - 用于 segue 动画的 CATransition(iOS 7 之前的样式)

objective-c - 将图像保存到核心数据