Search code examples
ioscore-data

What is the purpose of returnsObjectsAsFaults


I do not really understand the usage of "returnsObjectsAsFaults". The documentation says:

A Boolean value that indicates whether the objects resulting from a fetch using the receiver are faults.

YES if the objects resulting from a fetch using the receiver are faults, otherwise NO. The default value is YES. This setting is not used if the result type (see resultType) is NSManagedObjectIDResultType, as object IDs do not have property values. You can set returnsObjectsAsFaults to NO to gain a performance benefit if you know you will need to access the property values from the returned objects.

By default, when you execute a fetch returnsObjectsAsFaults is YES; Core Data fetches the object data for the matching records, fills the row cache with the information, and returns managed object as faults. These faults are managed objects, but all of their property data resides in the row cache until the fault is fired. When the fault is fired, Core Data retrieves the data from the row cache. Although the overhead for this operation is small, for large datasets it may become non-trivial. If you need to access the property values from the returned objects (for example, if you iterate over all the objects to calculate the average value of a particular attribute), then it is more efficient to set returnsObjectsAsFaults to NO to avoid the additional overhead.

What does it mean that by returning managed object as faults?

So if it is set to true, the objects will be stored in a cache and be inaccessible?

On the other hand false will make it accessable?

Please help me clarity this as I am very confused by the documentation's explanation.

let request = NSFetchRequest(entityName: "BlogItems")
request.returnsObjectsAsFaults = false

Solution

  • It looks like you answered your own question. More from documentation:

    A managed object fault is an instance of the appropriate class, but its persistent variables are not yet initialized

    Read Faulting and Uniquing section of Core Data Programming Guide.