DevPrep

All topics › Databases & storage

What are faulting and lazy loading in Core Data?

Middle PRO

Essence

A fault is a placeholder: an NSManagedObject whose objectID and type are known but whose properties haven't been loaded from the store yet. A fetch returns faults (unless returnsObjectsAsFaults = false is set), and on the first attribute access the fault "fires": Core Data queries the store (or uses the coordinator's row cache) and populates the object. Relationships are faults too: to-one is a fault on an object, to-many a fault on a Set.

That is lazy loading in Core Data: memory is spent only on what's actually touched.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store