简体   繁体   English

如何处理核心数据保留周期

[英]How to deal with Core Data retain cycles

The core data guidelines recommend that you model your relationships with an inverse. 核心数据准则建议您使用逆模型来建立关系模型。 No problems there. 那里没有问题。

Interestingly though if you Load an object A that has a to many relationship to B and walk the object graph you end up with a retain cycle and the memory is never freed. 有趣的是,如果加载与A有很多关系的对象A并遍历对象图,则最终会产生一个保留周期,并且内存永远都不会释放。

For a simple object graph you can just call refreshObject:mergeChanges: on A to re-fault the object so that relationships are no longer strong references. 对于简单的对象图,您只需在A上调用refreshObject:mergeChanges:即可重新对对象进行故障处理,以便关系不再是强引用。

If you have a complicated object graph though this is a pain because you need to call it on every object you have touched. 如果您有一个复杂的对象图,那么这很麻烦,因为您需要在触摸过的每个对象上调用它。 It seems like a pretty important consideration when using core data yet there is only one paragraph on this topic in Apples documentation. 使用核心数据时,这似乎是一个非常重要的考虑因素,但是Apple文档中只有一个关于此主题的段落。

I am just wondering how other people handle this? 我只是想知道其他人如何处理? A long running app would slowly just consume more and more memory without some sort of manual process to force objects to revert to faults. 长时间运行的应用程序会慢慢消耗越来越多的内存,而无需进行任何手动过程来迫使对象恢复为故障。

Are there any known patterns for dealing with this. 是否有任何已知的模式来处理此问题。 I'd imagine so since lots of people use Core Data I just can't find any recommendations 我可以想象,既然很多人都在使用Core Data,我就是找不到任何建议

You are ignoring several aspects of core data when making your assertions. 进行断言时,您将忽略核心数据的多个方面。 If you fetch an object, let's say object A, which has a one-to-many relationship to object B, when you fetch A, you will have all the objects on B which are related to A. A one to many relationship creates the list of objects related to A and contains them on an NSSet property of your NSManagedObject subclass. 如果获取一个对象,比方说与对象B具有一对多关系的对象A,则在获取A时,将拥有B上与A相关的所有对象。一对多关系创建了对象A。与A相关的对象列表,并将它们包含在NSManagedObject子类的NSSet属性上。 Note that these objects are in a faulted state, and the memory footprint from this is insignificant. 请注意,这些对象处于故障状态,并且由此引起的内存占用微不足道。 If you manipulate the objects in the relationship, core data will unfault these objects when necessary. 如果您操纵关系中的对象,则核心数据将在必要时取消对这些对象的破坏。 You do not have to do anything to get this behavior. 您无需执行任何操作即可获得此行为。 If you want to trigger the faulting behavior yourself to send the objects to fault again, you can use refreshObject:mergeChanges:. 如果要触发故障行为以再次将对象发送给故障,则可以使用refreshObject:mergeChanges:。 If you do not send them back to fault, the faulting behavior will be trigger again eventually. 如果不将它们发送回故障,则最终将再次触发故障行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM