-1

I keep hitting Core Data faults when updating and deleting objects and Id like to understand why. The logic essentially fetches the entities, updates a few fields and saves. No faults are hit. The entities are then consumed for some business logic and when thats done, the entities are deleted one by one using the following code: [context deleteObject:[context objectWithID:entity.objectID]]; performed within the context block. Why does the line of code trigger fault?

I did end up moving this to use NSBatchDeleteRequest where I passed it an array of object IDs and that got rid of the faults but just would like to understand why the faults got triggered in the first place.

2 Answers2

0

Faults are a normal part of Core Data.

When you execute a fetch request with resultType of managedObjectResultType (which is the default), objects are returned as faults, unless you explicitly request otherwise by setting returnObjectsAsFaults to `false.

The object properties are held in the row cache and faults are returned in the result set. When you access a property, the fault fires and the data is fetched from the row cache. This process is transparent and has a relatively low overhead.

If you know that you are going to access the object properties, and you have a large result set, then you can set returnsObjectsAsFaults to false to avoid the fault process.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

Why do you need to look up the object by ID? Is entity from a different context than context? If so, -objectWithID: will return a fault, so that’s probably the answer to your question. The fault is probably not a big deal because with entity already in memory (in the other context) the data will be in the row cache so the fault will not need to fetch from the store.

If you have only one context, -objectWithID: should return the exact same object as entity, which you said was not initially a fault, so the cause of the fault must be elsewhere. (Plus, you could just pass entity directly to -deleteObject:.)

Michael Tsai
  • 1,945
  • 17
  • 41