DevPrep

All topics › Databases & storage

What are the nuances of Core Data with threads?

Senior PRO

The main rule

An NSManagedObjectContext and all of its NSManagedObjects belong to one queue: the main context to the main queue, a private context to its own internal queue. Every access (reading a property, fetch, save) must go through that context's perform { } or performAndWait { }. Violations give non-deterministic crashes and data corruption, often far from the actual mistake.

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