在不删除用户数据的情况下迁移/更新核心数据应用程序! [英] migrate/ update core data app without erasing user data!

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

问题描述

我有一个非常复杂的问题想与您分享,也许有人可以为我解答.在我开始之前,我必须说我在这方面很新.

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

问题:我必须做什么才能:- 在不接触"用户数据的情况下更新存储在 sql 数据库中的一些(不是全部)默认数据?(模型将保持不变-没有新实体等-)(如果用户卸载应用程序然后重新安装新版本一切都会好的,但我不想这样做,显然).

有人可以在编码方面提供帮助吗?

解决方案

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

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

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

让我印象深刻的一件事是,当您想使用版本控制和迁移时,加载核心数据 NSManagedObjectModel 的方式会发生变化.如果没有迁移,您可能会这样:

NSManagedObjectModel *model = [NSManagedObjectModel mergeModelFromBundles: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天全站免登陆