核心数据版本控制 [英] Core Data Versioning manually

查看:120
本文介绍了核心数据版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个应用程式, iCollege ,现在我想建立应用程式更好。

测试期间,我想从备份还原数据。启动iCollege时,应用程序崩溃,因为它无法通过Core Data进行版本控制。我现在的问题是,是否可以手动修改Core Data文件,即通过现有文件的每个被管对象,并将对象适配到活动模型版本。

I developed an app, iCollege, and now I want to make the app much better.
While the testing I wanted to restore the data from a backup. At launching iCollege the app crashes because it could not be versioned by Core Data. My question now is if it is possible to version a Core Data file manually, i.e. going through every Managed Object of the existing file and fit the objects to the active Model Version.

有可能吗?

编辑:非常感谢你的快速和详细的答案。我认为我解释我的问题不是很好。这是我的问题的另一个描述。

我有一个使用Core Data创建的文件,其中包含实体 Course 教师。我用这个文件替换我的应用程序的Core Data文件。当应用程序下次启动时,我想调用一个方法读取文件的管理对象与课程教师并将其转换为实体,例如主题教授。属性也可以改变。我的问题是,如果我能做到这一点,即我可以自己版本的核心数据文件?

我希望我的问题是现在可检查。 : - )

Thank you very much for your fast and detailed answer. I think that I explained my question not very good. Here is another description of my problem.
I have a file created with Core Data with a few Objects of the entities Course and Teacher. I replace the Core Data file of my app with this file. When the app launches the next time I want to call a method which reads the Managed Objects of the file with the objects of Course and Teacher and convert it into the Entities, e.g. Subject and Professor. The attributes may be changed, too. My question is if I can do this, i.e. can I version the Core Data file on my own?
I hope my question is now checkable. :-)

推荐答案

您可以执行以下操作。核心数据允许您向 NSPersistentStoreCoordinator 添加多个持久性存储,因此您需要两个与协调器关联的商店。请注意,不可能使用两个不同的管理对象模型(MOM),您的所有商店只能有一个MOM。

You can do this as follows. Core data allows you adding multiple persistent stores to a NSPersistentStoreCoordinator, so you need two stores associated to your coordinator. Note that it is not possible to use two different Managed Object Models (MOMs), you can only have one MOM for all of your stores.

虽然这看起来相当困难,但是相当简单,因为Core Data也允许使用配置。配置具有名称和相关联的实体集。这些集合可以重叠 - 即,给定实体可以出现在多于一个配置中。请参阅相关的文档

While this may appear to be rather difficult, it is instead simple enough, given that Core Data also allows using configurations. A configuration has a name and an associated set of entities. The sets may overlap—that is, a given entity may appear in more than one configuration. See the associated documentation

现在,您可以通过在不同的商店中使用不同的实体轻松处理您的问题,
创建一个模型,它将是所有你要去的实体
处理(或至少是联合),并定义你需要的子集作为配置。

You can now easily deal with your problem by having different entities in different stores, creating a model which will be a superset of all the entities you're going to deal with (or at least the union) and that defines the subsets you need as configurations. Then, for each individual store, you specify the associated configuration.

类似的东西(只是一个示例代码片段):

Something like (just an example snippet):

NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] init];
[coordinator addPersistentStoreWithType:type configuration:@"CourseAndTeacher" URL:aURL options:nil error:NULL];
[coordinator addPersistentStoreWithType:type configuration:@"SubjectAndProfessor" URL:anotherURL options:nil error:NULL];

NSManagedObjectContext *context = [[NSManageObjectContext alloc] init];
[context setPersistentStoreCoordinator:coordinator];

这篇关于核心数据版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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