核心数据和保留周期 [英] Core Data And Retain Cycles

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

问题描述

我有一个核心数据类游戏,它与另一个类Player有一对多的关系。

I have a core data class Game which has a to-many relationship to another class Player. This is what their headers look like

@property (nonatomic, retain) NSSet * players; // In Game.h
@property (nonatomic, retain) Game * game; // In Player.h (the inverse relationship)

当我释放最后一个外部引用有到Game类,didTurnIntoFault没有被调用。现在,我的问题是,这可能是由于上面创建的循环引用(如你所见,两个属性都是'retain'),或核心数据管理所有这一切,问题在我的代码中的某个地方。

When I am releasing the last external reference that I have to the Game class, didTurnIntoFault is not being called. Now, my question is that could this be due to the cyclic reference created above (As you can see, both the properties are 'retain'), or does core data manage all that and the problem is somewhere in my code.

推荐答案

请参阅核心数据编程指南:内存管理(断开关系保持循环)


当有托管对象之间的关系时,每个对象维护一个对它的相关对象的强引用。在受管理的内存环境中,这会导致保留周期(请参阅对象所有权和处置),可以阻止释放不需要的对象。为了确保保留循环被破坏,当您完成对象时,您可以使用受管对象上下文方法 refreshObject:mergeChanges:

When you have relationships between managed objects, each object maintains a strong reference to the object or objects to which it is related. In a managed memory environment, this causes retain cycles (see Object Ownership and Disposal) that can prevent deallocation of unwanted objects. To ensure that retain cycles are broken, when you're finished with an object you can use the managed object context method refreshObject:mergeChanges: to turn it into a fault.

您通常使用 refreshObject:mergeChanges:刷新托管对象的属性值。如果 mergeChanges 标志为 YES ,该方法将对象的属性值与持久存储中可用的对象的属性值合并协调员。然而,如果标志 NO ,则该方法简单地将对象变回故障而不合并,这使得它释放相关的被管理对象。这会中断该管理对象与其保留的其他管理对象之间的保留周期。

You typically use refreshObject:mergeChanges: to refresh a managed object's property values. If the mergeChanges flag is YES, the method merges the object's property values with those of the object available in the persistent store coordinator. If the flag is NO, however, the method simply turns an object back into a fault without merging, which causes it to release related managed objects. This breaks the retain cycle between that managed object and the other managed objects it had retained.

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

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