迁移/更新核心数据应用程序而不删除用户数据!

发布于 2024-08-31 15:56:23 字数 330 浏览 2 评论 0原文

我有一个非常复杂的问题,我想与您分享,也许有人可以帮我回答。 在开始之前我必须说我对此很陌生。

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

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

有人可以在编码级别提供帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他夏了夏天 2024-09-07 15:56:23

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

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];

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];
平定天下 2024-09-07 15:56:23

核心数据明确支持模型版本控制,并提供在版本之间迁移数据的工具。这应该包含您需要的信息。
用于迁移的开发者文档链接

Core data explicitly supports model versioning and provides facilities to migrate your data between versions. This should contain the information you need.
Link to Developer Docs for Migration

初见 2024-09-07 15:56:23

由于您已输入默认数据,因此您必须知道这些记录的 ID/密钥。您所需要的只是一个可以更改默认数据的更新脚本。

Since you have entered the default data, you must be knowing the Ids/keys for those records. All you need is an update script that would change the default data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文