核心数据内存管理 [英] Core Data Memory Management

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

问题描述

我读过Core Data文档中的内存管理部分,我还是有点困惑。我有一个上下文在我的应用程序,我有几件事情从它的对象。例如,一些获取的结果控制器,详细视图和一些其他代码获取随机对象。一旦对象已经完全释放并且它们的保留计数为0,核心数据会自动释放所有对象信息并使它们故障?

I've read the memory management sections in the Core Data docs and I'm still a little confused. I have one context in my app, and I have several things getting objects out of it. For example a few fetched results controllers, detail views and some other code fetching random objects. Once objects have been fully released and their retain count is 0, will core data automatically release all the object information and fault them?

我在一些取回的结果控制器中将大量数据拖入我的上下文中,我想确保在用户完成滚动并可能已经钻取

I'm pulling lots of data into my context in some of my fetched results controllers, and I want to make sure that after the user has finished scrolling and perhaps has drilled down into another view, will those objects that were fetched when scrolling the tableview be released and faulted back to the store?

非常感谢,

Mike

推荐答案

Core Data管理对象生命周期的方式与其他Cocoa管理对象生命周期的方式相同:NSManagedObject只要托管对象上下文或任何其他对象保留对它们的所有权(通过 - [NSObject retain] 。默认情况下, NSManagedObjectContext 不保留实例,因此它们会在任何其他所有者(即您的 NSFetchedResultsController 实例或程序中的其他实例)释放它们。您可以更改托管对象上下文的此默认行为以保留实例,但很少需要。受管对象上下文 用于保留在下次保存之前更新的实例。除非保存对象实例,否则没有办法保留这些更改。因此,为了最小化Core Data对象的内存使用,请遵循标准规则:尽快释放它们。如果您发现您的上下文内存使用情况正在增长(使用Instruments的核心数据工具来跟踪此情况),如果您更新实例并因此在下一次保存之前在上下文中保持它们活动,则更频繁地保存上下文,即使您有其他发布它们。

Core Data manages object lifetimes the same way the rest of Cocoa manages object lifetimes: NSManagedObject instances in a managed object context are retained in memory as long as the managed object context or any other object retains ownership of them (via -[NSObject retain]. By default, the NSManagedObjectContext does not retain instances, so they are released as soon as any other owners (i.e. your NSFetchedResultsController instances or other instances in your program) release them. You can change this default behavior of the managed object context to retain instances, but you rarely want to. The managed object context has to retain instances that are updated until the next save. There's no way to preserve these changes except in the object instance until the context is saved. So, to minimize memory usage of Core Data objects, follow the standard rules: release them as soon as you can. If you find that your context memory usage is growing (use Instruments' Core Data instruments to track this), save the context more frequently if you are updating instances and hence keeping them alive in the context until the next save even if you've otherwise released them.

使用 NSFetchedResultsController 使所有这一切更容易。事实上, NSFetchedResultsController 存在的原因是在低内存环境(如iPhone)中批量提取对于程序员来说更容易。

Using NSFetchedResultsController makes all of this easier. In fact, the reason NSFetchedResultsController exists at all is to make batch fetching in a low memory environment (like the iPhone) easier for the programmer.

正如Louis所提到的, NSPersistentStoreCoordinator 维护一个行缓存来缓存内存中的实例数据,而不是在对象出现故障时返回磁盘。管理对象上下文。这是一个核心数据实现细节,但是(虽然缓存未命中是性能命中;你可以跟踪在仪器中的缓存未命中)。 Core Data管理缓存内存,您不必担心它。

As Louis mentioned, the NSPersistentStoreCoordinator maintains a row cache to cache instance data in memory instead of having to go back to disk when an object is faulted into the managed object context. This is a Core Data implementation detail, however (though cache misses are a performance hit; you can track cache misses in Instruments). Core Data manages the cache memory and you shouldn't have to worry about it.

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

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