迁移/更新核心数据应用程序,而不擦除用户数据! [英] migrate/ update core data app without erasing user data!

查看:167
本文介绍了迁移/更新核心数据应用程序,而不擦除用户数据!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常复杂的问题,我想和你分享,也许有人可以为我回答。
在我开始之前,我不得不说,我是非常新的在这。



所以,我有一个coredata iphone应用程序使用预填充的sql数据库。
用户可以添加/编辑自己的数据,但默认数据不能删除。 useres数据全部保存在同一个sql数据库中。



QUESTION:
我需要做什么才能:
- 更新存储在sql数据库中的一些(而不是全部)默认数据,而不触摸用户的数据? (模型将保持不变 - 没有新的实体etc-)
(如果用户卸载应用程序,然后重新安装新版本一切都会确定,但我不想这样做,显然)。


解决方案

要支持添加新实体等。稍后,您要使用版本控制和自动轻量级迁移,如下所示:



http://developer.apple.com/iphone/library /documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweight.html#//apple_ref/doc/uid/TP40008426-SW1



基本上你创建一个新的版本的数据模型,使用Xcode中的 Design-> Data Model 菜单项,然后进行一些代码更改。这将导致Core Data自动将较旧的模型迁移到较新的模型。您可以在哪些类型的更改中进行限制。您可以添加新实体,并向现有实体添加可选属性,或者设置默认值的必填属性。



我吸引了我的是,当您要使用版本控制和迁移时,核心数据 NSManagedObjectModel 会发生更改。没有迁移你可能有这样:

  NSManagedObjectModel * model = [NSManagedObjectModel mergedModelFromBundles:nil]; 

一旦开始使用版本控制和迁移,这需要更改为:

  NSString * path = [[NSBundle bundleForClass:self.class] pathForResource:@DataModelName
ofType:@momd];
NSURL * url = [NSURL fileURLWithPath:path];
NSManagedObjectModel * model = [[[NSManagedObjectModel alloc] initWithContentsOfURL:url] autorelease];


i have a very complicated problem that i would like to share with you and maybe someone can answer it for me. before i start i have to say that i am very new in this.

So, i have a coredata iphone app (much like the recipes app) that uses a pre-populated sql database. The user can add/edit his own data but the default data cannot be deleted. the useres data are ALL saved in the same sql database.

QUESTION: what do i have to do in order to: - update some (not all) of the default data that are stored in the sql database without "touching" the user's data? (the model will stay the same - no new entities etc-) (if the user uninstall the app and then reinstall the new version everything will be ok but i dont want to do this, obviously).

can someone PLEASE help in coding level?

解决方案

To support adding new entities etc. later on, you want to use versioning and automatic light-weight migration which is described here:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweight.html#//apple_ref/doc/uid/TP40008426-SW1

Basically you create a new version of your data model using the Design->Data Model menu item in Xcode, then make a few code changes. This will cause Core Data to automatically migrate an older model to the newer one. You are limited in what kinds of changes you can make. You can add new entities, and either add optional attribute to existing entities, or required attributes with default values set.

One thing that caught me out is that the way you load the core data NSManagedObjectModel changes when you want to use versioning and migration. Without migration you probably have this:

NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];

Once you start using versioning and migration this needs to change to something like this:

NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:@"DataModelName"
                                                                ofType:@"momd"];
NSURL *url = [NSURL fileURLWithPath:path];
NSManagedObjectModel *model = [[[NSManagedObjectModel alloc] initWithContentsOfURL:url] autorelease];

这篇关于迁移/更新核心数据应用程序,而不擦除用户数据!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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