Core Data 可以处理我的“系统数据与用户数据”吗?迁移需求?

发布于 2024-09-10 09:15:52 字数 638 浏览 2 评论 0原文

我的 iPhone 应用程序将具有只读“系统”数据和读/写“用户”数据(使用 Core Data 或自定义 SQLite 数据库存储)。用户数据可以参考系统数据。当安装新版本的应用程序时(例如,通过 iTunes):

  • 更新附带的系统数据应覆盖/替换系统数据
  • 用户数据应修改为引用系统数据(如果可能)。

问题:Core Data 的这种迁移是如何完成的?可行吗?

例如,假设我的应用程序用于管理菜谱。

  • 该应用程序的每个版本都会附带一组默认的食谱。
  • 用户不能编辑这些“官方”食谱。
  • 但是,开发人员可以在应用程序的未来版本中修改(或删除)任何“官方”食谱。
  • 用户可以向“官方”食谱添加注释/评论(例如,“烘烤 45 分钟而不是 30 分钟”)。

当用户升级到应用程序的新版本时,我们希望保留用户评论,并尝试将它们与新的“官方”食谱集中的匹配食谱重新关联。对于核心数据来说这可能/可行吗?或者也许我应该使用普通的“数据库”解决方案(例如,SQLite 和传统的创建/读取/更新/删除操作)?

谢谢!

My iPhone app will have read-only "system" data AND read/write "user" data (stored either using Core Data or a custom SQLite db). The user data may reference the system data. When a new version of the app is installed (e.g., via iTunes):

  • The new system data that comes with the update should overwrite/replace the old system data
  • The user data should be modified to reference the new system data (where possible).

Question: How is this kind of migration done with Core Data? Is it feasible?

For example, let's say my application is for managing recipes.

  • Each version of the app will come with a default set of recipes.
  • The user can not edit these "official" recipes.
  • However, the developer may modify (or delete) any "official" recipes in future versions of the app.
  • Users are allowed to add notes/comments to the "official" recipes (e.g., "bake for 45 min. instead of 30").

When the user upgrades to a new version of the app we want to keep the user comments and try to re-associate them with matching recipes from the new, "official" set of recipes. Is this possible/feasible with Core Data? Or perhaps I should just use a plain "database" solution (e.g., SQLite and traditional create/read/update/delete operations)?

Thanks!

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

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

发布评论

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

评论(1

梦断已成空 2024-09-17 09:15:52

您应该有两个持久存储。捆绑包中的只读存储和文档目录中的读/写存储。

您可以将这两个存储添加到 NSPersistentStoreCoordinator 中,并从一个 NSManagedObjectContext 访问它们。

如果两个存储具有相同的实体,那么您将需要调用 -assignObject:toPersistentStore: 来告诉 Core Data 将实体保存到哪个存储中。如果每个底层模型都有不同的实体,那么这是没有必要的。

此外,您可以通过确保每个配方都有唯一的标识符(您创建的)并且注释存储该标识符,以便您可以使用获取的属性来检索关联的配方并关联注释,从而将注释“链接”到只读配方。

ObjectID

首先,不要使用 -objectID 在商店之间进行链接。它可以而且确实在迁移期间(和其他时间)发生变化,这将使您的迁移比需要的更加丑陋。

迁移

迁移非常简单。如果您需要更改只读数据模型,只需更改它并将新版本包含在您的应用程序中即可。

如果您需要更改读写模型,请创建一个新模型,在测试期间使用自动迁移,并在准备交付时编写一个从旧版本到新版本的 NSMappingModel

由于两个持久性存储(及其关联的模型)没有链接,因此迁移的风险很小。一个“问题”是 Core Data 的模板代码将无法自动解析用于迁移的源模型。要解决此问题,您需要稍微介入并帮助解决:

  1. 启动您的目标 NSPersistentStoreCoordinator 并注意是否有错误。如果出现迁移错误:
  2. 查找源模型并使用所有适当的源模型创建 NSManagedObjectModel 实例。
  3. 创建 NSMigrationManager 实例并为其指定源模型和目标模型
  4. 调用 - migrateStoreFromURL: type: options: withMappingModel: toDestinationURL: destinationType: destinationOptions: error: 开始迁移
  5. 利润!

以这种方式处理迁移需要做一些额外的工作。如果您的两个模型非常分离,您可以做一些不同的事情,但这需要测试(就像所有事情一样):

  1. 捕获迁移错误
  2. 建立一个新的NSPersistentStoreCoordinator 只是需要迁移的持久存储(和模型)。
  3. 让那个人迁移吧。
  4. 拆除该 NSPersistentStoreCoordinator 并尝试再次启动您的主 NSPersistentStoreCoordinator

You should have two persistent stores. A read only store that is in your bundle and a read/write store that is in the documents directory.

You can add both stores to the NSPersistentStoreCoordinator and access them both from one NSManagedObjectContext.

If both stores have the same entity then you will want to call -assignObject:toPersistentStore: to tell Core Data which store to save the entity into. If each underlying model has different entities then this is not necessary.

In addition you can "link" notes to a read-only recipe by making sure each recipe has a unique identifier (that you create) and the note stores that so that you can use a fetched property to retrieve the associated recipe and associates notes.

ObjectID

First, do not use the -objectID for linking between stores. It can and does change during migration (and other times) which will make your migration MUCH uglier than it needs to be.

Migration

Migration is very straight-forward. If you need to change the read-only data model, just change it and include the new version with your application.

If you need to change the read-write model, create a new model, use automatic migration during testing and when you are ready to ship, write a NSMappingModel from the old version to the new version.

Because the two persistent stores (and their associated models) are not linked there is very little risk with migration. The one "catch" is that the template code for Core Data will not be able to automatically resolve the source model for migration. To solve this issue you need to step in a little bit and help it out:

  1. Stand up your destination NSPersistentStoreCoordinator and watch for an error. If you get a migration error:
  2. Find the source model(s) and create an instance of NSManagedObjectModel with all of the appropriate source models.
  3. Create an instance of NSMigrationManager and give it the source and destination models
  4. Call - migrateStoreFromURL: type: options: withMappingModel: toDestinationURL: destinationType: destinationOptions: error: to kick off the migration
  5. Profit!

It is a bit more work to handle the migration in this way. If your two models are very separated, you could do it a little different but it will require testing (as all things do):

  1. Catch the migration error
  2. Stand up a new NSPersistentStoreCoordinator with just the persistent store (and model) that needs to migrate.
  3. Let that one migrate.
  4. Tear down that NSPersistentStoreCoordinator and attempt to stand up your main NSPersistentStoreCoordinator again.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文