Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does fetching object also fetches relationship objects in core data

Tags:

ios

iphone

I have one to many relationship b/w objects in core data. Like A==>>B(many objects) So A is has one to many relationship with B objects. My Question is that when I fetch A object then does B objects also gets loaded into memory? If No, then when they gets loaded? When I access relationships?

like image 340
user1923809 Avatar asked Feb 27 '13 12:02

user1923809


1 Answers

In most cases it doesn't. This is a mechanism called Faulting in Core Data. The framework takes care of realizing faults behind the scene when you ask for an object that has not yet been realized (i.e. loaded in memory).

Fault handling is transparent—you do not have to execute a fetch to realize a fault. If at some stage a persistent property of a fault object is accessed, then Core Data automatically retrieves the data for the object and initializes the object (see NSManagedObject Class Reference for a list of methods that do not cause faults to fire). This process is commonly referred to as firing the fault. If you send the Department object a message to get, say, its name, then the fault fires—and in this situation Core Data executes a fetch for you to retrieve all the object's attributes.

So in your example, if you load A, Core Data will fault the B instances (i.e. not load them into memory) and when you actually try to access B, then it realizes the fault (i.e. loads into memory).

Conversely, there are times when you have loaded objects in memory and you would like to "unload" them. This is called turning objects into faults.

You can turn a realized object into a fault with the refreshObject:mergeChanges: method. If you pass NO as the mergeChanges argument, you must be sure that there are no changes to that object’s relationships. If there are, and you then save the context, you will introduce referential integrity problems to the persistent store.

like image 59
mprivat Avatar answered Sep 18 '22 23:09

mprivat