更改CoreData模型:复古兼容 [英] Changing CoreData Model : retro compatibility

查看:111
本文介绍了更改CoreData模型:复古兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用coredata打这个问题,因为它应该是直接的。



我目前正在处理这个应用程序的第一个版本,os显然我在这里和那里继续tweeking核心数据模型



然而每次更改核心数据模型,我需要卸载应用程序并重新安装新版本。 / p>

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



我缺少什么,



有一些代码我需要写,告诉核心数据如何修改



Jason

解决方案

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



步骤:



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



2)转到该模型版本并更改您的模型。



3)现在转到 YourProjectModel。



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

  NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,

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

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

  [__ persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:& error] 

在我的例子中,它看起来像这样:

   - (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;
}

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


I've been hitting this problem with coredata and it's driving me nuts because it should be straight forwards

I'm currently working on the first release of this app, os obviously I keep tweeking the core data model here and there,

However each time change the core data model I need to uninstall the application and reinstall the new version.

This is passable while its just me, but once released I need to be able to change update the app without my users reinstalling.

What am I missing,

Is there some code I need to write to tell core data how to modify the existing persistant data to the new one ?

THanks for your help

Jason

解决方案

Core data model - migration - adding new attributes/fields to current data model - no RESET of simulator or app required

Steps:

1) Create Model version from editor - Give it any meaningful name like ModelVersion2

2) Go to that model version and make changes to your model.

3) Now go to YourProjectModel.xcdatamodeld and set current version to newly created version.

4) Add below code to place where you are creating persistent coordinator -

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 

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

and set options value as options for method -

[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]

In my case, it looks something like this:

- (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;
 }

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

这篇关于更改CoreData模型:复古兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆