tags:

views:

75

answers:

1

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)

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.

+1  A: 

See Core Data Programming Guide: Memory Management (Breaking Relationship Retain Cycles).

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.

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.

Matt B.
I can swear that I RTFM, yet that missed me :) Thanks anyway!
KnickerKicker
Might I add that you have to do this recursively i.e. breaking the cycles for a managed object DOES NOT automatically break any cycles that the children objects may have amongst themselves
KnickerKicker