Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is CoreData faulting?

Can any one explain what coredata faulting means? I understood that it's a mechanism to reduce memory. But my question is, if when we try to use a faulted object, do we need to call any refresh methods or will CoreData picks the values for us? If CoreData handles it for us, what will happen if the faulted object is deleted from actual persistent store and we try to access it through faulted object? Will it throw any exception?

like image 665
Advaith Avatar asked Jan 31 '13 20:01

Advaith


2 Answers

In Core Data, faults are placeholders, or “unrealized objects”. They are small objects which refer to other NSManagedObjects, which are fetched into memory only as needed. This faulting mechanism is designed to enhance performance and reduce memory use.

In general, the faulting mechanism is transparent; when you retrieve an object from an NSManagedObjectContext (MOC) you can’t tell (in the normal course of its use) whether it’s a fault or a realized object. A fault will be converted into a realized object (“fired”) automatically by the Core Data framework in most cases when it is necessary to do so, e.g. when accessing a property of the object. If you need to fire a fault yourself, you can do so by invoking its willAccessValueForKey: method with a nil argument.

like image 187
Dhruv Goel Avatar answered Sep 28 '22 04:09

Dhruv Goel


Great answer from Dhruv! In answer to your final question, if you try to access a managed object which is first faulted then deleted, you will see an NSObjectInaccessibleException and the message "Core Data could not fulfill a fault"

like image 26
David N. Foote Avatar answered Sep 28 '22 04:09

David N. Foote