在执行Core Data迁移时,出现内存不足 [英] Out-Of-Memory while doing Core Data migration

查看:136
本文介绍了在执行Core Data迁移时,出现内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个应用程序的两个版本之间迁移CoreData模型。我正在存储二进制数据作为blobs在以前的版本,我想把它们从blobs的性能。我的问题是,在迁移期间,似乎Core Data将所有内容加载到内存,导致低内存警告,然后到我的应用程序被杀。

I'm migrating a CoreData model between two versions of an application. I was storing binary data as blobs in the previous version and I want to take them out of the blobs for performance. My issue is that during the migration it seems that Core Data loads everything into memory which leads to Low Memory Warnings and then to my app being killed.

Apple文档建议以下:
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmCustomizingTheProcess.html#//apple_ref/doc/uid/TP40005510-SW9

Apple documentation suggests the following : http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmCustomizingTheProcess.html#//apple_ref/doc/uid/TP40005510-SW9

但是,似乎依赖于大对象应用不同映射的事实。
在我的例子中,所有对象基本上是一样的,并且必须对每个对象应用相同的映射。

However, it seems to rely on the fact that the large objects are applied different mapping. In my case, all the objects are basically the same and the same mapping has to be applied to each of them. I don't see in this case how I could apply their technique.

我应该如何处理大型对象的迁移?

How should I handle a migration with very large objects ?

推荐答案

我猜想除了从blob中提取数据之外,你还需要做一些更改。我的建议是在几个阶段进行迁移。我在这里大声地想,所以它可能有可能改进这一点。这需要你使用SQLite。

I'm guessing that you have a bunch of changes you want to make in addition to pulling the data out of blobs. My suggestion is to do the migration in a few stages. I'm kind of thinking out loud here, so it might be possible to improve on this. This requires you to be using SQLite.

要使这个工作,你将有三个版本的模型:

To make this work, you're going to have three versions of your model:


  1. 原始模型

  2. 删除属性的模型(可能添加了特殊的唯一ID)
  3. 包含所有更改的模型,包括添加新实体和替换属性的关系

这样做的原因是,从版本1到2的转换应该可以通过自动轻量级迁移来实现。在这种情况下,Core Data不需要将任何内容加载到内存中 - 它只是发出SQL语句来直接对数据库进行更改。

The reason to do this is that the transition from version 1 to 2 should be doable with an automatic lightweight migration. In that case Core Data doesn't need to load anything into memory--it just issues SQL statements to make the changes directly on the database.

使用旧模型版本设置持久存储协调器。加载数据后,浏览所有要迁移的对象,解压缩二进制属性,然后将其写入磁盘。您可以使用具有批处理和常规自动释放池排空的读取请求,以确保不会占用过多的临时对象的内存。将数据存储到NSCachesDirectory中获得的目录。你显然需要以一种方式存储数据,让你将它与对象的managedObjectID关联起来。

So, you start by setting up your persistent store coordinator using the old model version. Once you've loaded the data, go through all of the objects you're migrating, extract the binary attribute, and write it to disk somehow. You can use a fetch request with batching and regular autorelease pool draining to make sure you don't use up too much memory for temporary objects. Store the data into the directory you get with NSCachesDirectory. You'll obviously want to store the data in a way that lets you relate it back to the object's managedObjectID.

然后,你关闭所有的事情并要求Core Data迁移存储从版本1到版本2.请参见此链接了解详情。使用版本2打开商店。

Then, you shut everything down and ask Core Data to migrate the store from version 1 to version 2. See this link for details. Open up the store with version 2.

您可能需要添加一个步骤,为每个对象分配一些唯一的ID,因为我不确定是否数据在执行非轻量级迁移时维护对象ID。如果你需要这样做,你的版本2模型将添加一个新的属性到你正在获取的二进制数据的对象将是可选的或有一个默认值集。由于轻量级迁移不应该更改managedObjectID,您可以将新的唯一ID的映射保存到您两年前保存的managedObjectID和二进制数据。

You might have to add a step where you assign some sort of unique ID to each object, because I'm not sure if Core Data maintains object IDs when it does a non-lightweight migration. If you need to do this, your version 2 model would add a new attribute to the object you're taking the binary data out of that would be either optional or have a default value set. Since lightweight migration shouldn't change the managedObjectIDs, you could at save the mapping of your new unique ID to the managedObjectIDs you saved along with the binary data two paragraphs ago.

保存数据并关闭商店。

打开商店并从版本2迁移到版本3,应该基本上是您在发布之前已经编写的代码的问题。打开商店后,添加您从版本1商店保存的所有对象,并使用您在过程中保存的数据设置关系。

Open the store and do a migration from version 2 to version 3, which should basically be the code you already had written before you posted the question. Once the store is open, add all of the objects you saved from the version 1 store and set up the relationships using the data you saved along the way.

简单,右?

这篇关于在执行Core Data迁移时,出现内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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