核心数据迁移xcode 4.2不会更新app store中已有的应用数据

我们正在使用我们的在线目录的核心数据及其在app store中可用的工作正常和应用程序现在我需要使用一些字段和属性来升级核心日期。 它迁移到新的,但已经存储在应用程序中的旧数据完全消失了。 我尝试了各种方法来使用此代码保留它

NSString *databaseFilePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"App_iOS.sqlite"]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:databaseFilePath error:NULL]; NSURL *storeUrl = [NSURL fileURLWithPath: databaseFilePath]; NSError *error = nil; if( ![[self persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration: nil URL:storeUrl options:nil error:&error]) { [__managedObjectContext insertObjects]; } else { [__managedObjectContext updatedObjects]; } 

我还没有得到解决方案来保留应用程序中的数据。 我在网上搜索这个大部分面临同样的问题,但我还没有收到好的解决方案

我现在发现这很简单 – 一旦你知道在哪里看。#在我的AppDelegate中我设置了NSPersistentStoreCoordinator – 你需要添加一些选项来告诉它处理自动迁移:

  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; NSError *error; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {   // Handle error  NSLog(@"Problem with PersistentStoreCoordinator: %@",error); } 

然后你需要在xCode中做:

 1. Select your xcdatamodeld file 2. Select the Editor Menu at the top - then choose Add Model Version 3. Now your xcdatamodeld file have two (modelname.xcdatamodel & modelname2.xcdatamodel ) . 4. Now modelname.xcdatamodel have the green check mark implies it is current version, but we need to change the modelname2.xcdatamodel as a current version 5. Select the xcdatamodeld file and then select the View Menu at the top - then Choose Utilities - then Choose the Show File Inspector is shown in right side of Xcode and then Select the Versioned Core Data Model - have Current(DropDownList) - select modelname2(the one you just made current version have green check mark). 6. Now when you install this version onto a device that has the old model - it will automatically upgrade that model to the new model. 

这看起来很棒,也很简单 – 但是我认为你需要在开发过程中小心改变模型 – 否则你将不得不为每个变化创建一个新版本。

我想我会做的是保留所有已更改的文件,然后一旦我准备好部署我的更新,我将删除所有中间文件,并使用最旧和最新的模型进行部署。

您需要使用数据迁移技术。 我不久前遇到了类似的任务。
有关于此的教程。 数据迁移。