更改CoreData模型:复古兼容性

Jas*_*ers 2 model core-data ios xcdatamodel

我一直用coredata来解决这个问题,这让我疯狂,因为它应该是直接前进的

我目前正在开发这个应用程序的第一个版本,显然我一直在调查核心数据模型,

但是,每次更改核心数据模型时,我都需要卸载应用程序并重新安装新版本.

这是可以接受的,而它只是我,但一旦发布,我需要能够更改更新应用程序,而无需我的用户重新安装.

我错过了什么,

是否需要编写一些代码来告诉核心数据如何将现有的持久数据修改为新数据?

谢谢你的帮助

贾森

use*_*780 6

核心数据模型 - 迁移 - 向当前数据模型添加新属性/字段 - 无需重置模拟器或应用程序

脚步:

1)从编辑器创建模型版本 - 给它任何有意义的名称,如ModelVersion2

2)转到该型号版本并对模型进行更改.

3)现在转到YourProjectModel.xcdatamodeld并将当前版本设置为新创建的版本.

4)添加以下代码以放置您创建持久协调器的位置 -

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 

[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
Run Code Online (Sandbox Code Playgroud)

并将选项值设置为方法的选项 -

[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]
Run Code Online (Sandbox Code Playgroud)

就我而言,它看起来像这样:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

 if (__persistentStoreCoordinator != nil) {
    return__persistentStoreCoordinator;
 }

 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

 [NSNumber numberWithBool:YES],      NSMigratePersistentStoresAutomaticallyOption,

 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];


 NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"LGDataModel.sqlite"];

  NSError *error = nil;
  __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  if (!   [__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options  error:&error])
 {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
  }

  return__persistentStoreCoordinator;
 }
Run Code Online (Sandbox Code Playgroud)

链接:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmInitiating.html#//apple_ref/doc/uid/TP40004399-CH7-SW1