Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data And Retain Cycles

Tags:

core-data

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.

like image 952
Manav Avatar asked Nov 24 '09 13:11

Manav


People also ask

What is a retain cycle?

A retain cycle (also known as a reference cycle) is when two objects hold a strong reference to one another. An example of this is shown below: Object A is holding a strong reference to Object B , and Object B is also holding a strong reference to Object B .

What is meant by core data?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

What is core data on iPhone?

Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organized by the relational entity–attribute model to be serialized into XML, binary, or SQLite stores.

What is core data used for?

Overview. Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.


1 Answers

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.

like image 144
mbauman Avatar answered Oct 14 '22 01:10

mbauman