iOS8.1核心数据轻量级+重量级迁移 [英] iOS8.1 Core-Data Lightweight + Heavyweight Migration

查看:54
本文介绍了iOS8.1核心数据轻量级+重量级迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的核心数据数据库的版本1.(简化示例)

I have Version 1 of my core-data database. (Reduced example)

我对模型进行了一些更改,制作了V2.这涉及使用 type 属性以及其他一些属性创建一个新实体. type 属性是指向 plate 实体的链接.

I make some changes to the model, making V2. This involves creating a new entity with type attribute plus a few others. The type attribute is the link to the plate entity.

我的应用程序的新版本已发布,并且数据可以轻而易举地迁移.由于当时我自己的原因,我目前没有创建关系.

The new version of my app is released and the data migrates ok as this is lightweight. For my own reasons at the time, I did not create a relationship at this time.

有时候,我决定对结构进行更大的更改,创建新的实体 FixtureType PlateTypeImage .然后,我创建一些关系.这样就得到了我的模型的V3.

Sometime later I decide to do some bigger changes to the structure, creating new entities FixtureType and PlateTypeImage. I then create some relationships. This gives me V3 of my model.

由于这种修改的性质,我需要进行从V2到V3的重量级迁移,这涉及到复制属性数据,填充新属性以及在进行过程中设置关系.因此,我建立了一个映射模型,创建了必要的迁移策略,然后单击执行"按钮.

Because of the nature of this modification I need to do heavyweight migration from V2 to V3 which involves copying attribute data, populating new attributes and setting the relationships as I go. So, I set up a mapping model, create my necessary migration policies and hit the go button.

这适用于V2到V3,但是在测试从V1到V3的迁移时,出现了一系列错误...,例如

This works for V2 to V3 but when testing a migration from V1 to V3, I get a series of errors..., such as

reason =无法迁移就地存储:强制目标关系上缺少属性值的验证错误

我正在使用以下PSC选项:

I'm using the following PSC options:

NSDictionary *options = @{
                          NSMigratePersistentStoresAutomaticallyOption : @YES,
                          NSInferMappingModelAutomaticallyOption : @YES
                          };


if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

那么,我的查询是核心数据如何迁移数据?是顺序执行的,所以我可以从V1轻量化到V2,然后是重量级的V2到V3,还是从V1到V3迁移?如果是这样,我是否需要创建一个从V1到V3的迁移策略(很快就变得难以处理以覆盖所有组合)?

So, my query is how does core-data migrate the data? Is it sequentially, so I get a lightweight migration from V1 to V2, then heavyweight V2 to V3, or does it do a migration from V1 to V3? If so, do I need to create a migration policy for V1 to V3 (making things unwieldy very quickly to cover every combination)?

而且,一旦我开始使用重量级的软件,我现在是否已经失去了轻量级的迁移功能?

Also, have I now lost the lightweight migration facility, once I start using heavyweight?

建议和评论表示赞赏.

推荐答案

Core数据版本控制不是临时的.它只知道来源和目的地.因此,一旦引入新模型,就需要从以前的所有模型测试到当前模型.

Core Data versioning is not temporal. It only knows source and destination. Therefore, as soon as you introduce a new model you need to test from all previous models to the current model.

如果添加V4,则需要测试:

If you add V4, then you need to test for:

V1-> V4
V2-> V4
V3-> V4

V1->V4
V2->V4
V3->V4

因此,如果您的V4需要大量迁移,则需要为每个可能的迁移做一个映射.

So if your V4 requires a heavy migration then you need to do a map for each possible migration.

我的一般建议是不惜一切代价避免大量迁移.它们不适合在iOS上使用,并且经常引起问题.有一些替代方案会更好.

My general recommendation is to avoid heavy migrations at all costs. They are not designed to work on iOS and frequently cause issues. There are alternatives that will perform better.

我用来代替繁重迁移的两种最常见的方法是

Two most common approaches I use instead of heavy migration is

  1. 导出/导入.我为此使用JSON.它在内存上更容易,因此可以避免由于内存限制而导致崩溃.

  1. Export/Import. I use JSON for this. It is easier on memory and therefore can avoid crashes due to memory constraints.

巧妙的迁移前后代码.例如,如果要将数据拆分到一个新对象中(这需要大量迁移),则可以创建新对象并将数据保留在旧对象中.这将使它成为轻量级的迁移.您可以从那里监视迁移,然后在迁移完成后手动移动数据.您甚至还可以再轻量级迁移到不包含即将删除的属性的最终模型.

Clever pre and post migration code. For example, if you are splitting data out to a new object, something that requires a heavy migration, you could create the new object and leave the data in the old object. This would turn it into a lightweight migration. From there you could watch for the migration and then move the data manually after the migration is complete. You could even then do another lightweight migration to a final model that doesn't include the soon to be removed attributes.

请记住,iCloud不允许繁重的迁移,因此,如果您打算考虑使用iCloud,则必须跳过繁重的迁移.这也是一个非常有力的指标,表明苹果正在减慢过时的迁移速度,并将其作为最后的手段"进行处理.

Keep in mind that iCloud doesn't allow heavy migration so if you are ever going to consider using iCloud then you must skip heavy migration. This is also a very strong indicator that Apple is slowing deprecating heavy migration and leaving it in place as a "last resort" maneuver.

这篇关于iOS8.1核心数据轻量级+重量级迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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