核心数据可以处理我的“系统与用户数据”迁移需求? [英] Can Core Data handle my "system vs. user data" migration needs?

查看:201
本文介绍了核心数据可以处理我的“系统与用户数据”迁移需求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程式将拥有「只读」「系统」资料,以及「读取/写入」「使用者」资料(使用Core Data或自订的SQLite资料库储存)。用户数据可以参考系统数据。安装新版应用程式时(例如透过iTunes):

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).

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

例如,我的应用程序用于管理食谱。

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



  • 用户可以编辑这些官方食谱。

  • 但是,开发者可能会在未来版本的应用中修改(或删除)任何官方食谱。

  • / comment给官方食谱(例如,烘烤45分钟而不是30)。

  • 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").

到新版本的应用程序,我们想保留用户的评论,并尝试重新关联他们与匹配的食谱从新的,官方的食谱集。这是可能/可行的核心数据?或者我应该使用一个简单的数据库解决方案(例如,SQLite和传统的创建/读/更新/删除操作)?

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)?

em>

推荐答案

您应该有两个永久存储。

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.

您可以将这两个商店添加到中的一个只读存储库和一个位于文档目录中的读/ NSPersistentStoreCoordinator 并从一个 NSManagedObjectContext 访问它们。

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

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

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 在商店之间进行链接。在迁移(以及其他时间)期间,它可以并确实发生更改,这将使您的迁移 MUCH 比所需的更多。

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 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.

如果需要更改读写模型,创建一个新模型,在测试期间使用自动迁移,当您准备好发货时,将一个 NSMappingModel 从旧版本写入新版本。

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.

因为两个永久存储(及其关联模型)未链接,迁移的风险很小。一个catch是核心数据的模板代码将无法自动解析源模型进行迁移。要解决这个问题,您需要一点点帮助:

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. 站起来您的目的地 NSPersistentStoreCoordinator 并观察错误。如果发生迁移错误:

  2. 查找源模型并创建一个 NSManagedObjectModel 的实例,

  3. 创建 NSMigrationManager 的实例,并为其提供源和目标模型
  4. 调用 - migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:启动迁移
  5. / li>
  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. 迁移错误

  2. 刚才立即启动 NSPersistentStoreCoordinator

  3. 转移 NSPersistentStoreCoordinator 并尝试再次站起来您的主要 NSPersistentStoreCoordinator

  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.

这篇关于核心数据可以处理我的“系统与用户数据”迁移需求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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