15

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?

asked Jan 31, 2013 at 20:20
0

3 Answers 3

34

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.

answered Jan 31, 2013 at 20:33
Sign up to request clarification or add additional context in comments.

1 Comment

" 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" - Yes we can, use -isFault
5

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"

answered Feb 1, 2013 at 17:56

2 Comments

hey thank u :) one more query, I need to check if a managedObject is faulted or not, can I check it using "isFault" method? What am doing now is, I will check whether an object is faulted using "isFault" method and if the object is faulted I will get the object from persistent store using "existingObjectWithID:" method. Is it a correct approach?
Normally you would rely on CoreData to automatically fulfill a fault for you when you access an attribute that hasn't been realized from the cache or the persistent store. I typically use existingObjectWithID when I know the objects identity, but not its provenance (i.e. whether its ManagedObjectContext was created on the same thread in which my code is being executed)
3

from Coredata reference (link):

Faulting reduces the amount of memory your application consumes. A fault is a placeholder object that represents a managed object that has not yet been fully realized, or a collection object that represents a relationship.

Luca D'Alberti
4,8693 gold badges27 silver badges45 bronze badges
answered May 19, 2015 at 3:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.