Skip to content
tezvyn:

Describe a pattern for background Core Data fetches and UI updates

Source: developer.apple.comMediumHow cards are made

Describe a pattern for background Core Data fetches and UI updates

This tests NSManagedObjectContext concurrency and queue confinement. Strong answer: create a background context, fetch, pass objectIDs or structs to main, then main context fetches by ID to update UI. Red flag: passing NSManagedObjects across threads.

What's really being asked

Core Data concurrency and queue confinement. Interviewers want to see that you know an NSManagedObjectContext is bound to the queue it was created on, that NSManagedObjects are not thread-safe, and that crossing these boundaries crashes or corrupts data. They also care whether you know the modern NSPersistentContainer pattern versus the older manual parent-child or independent context setups.

The full answer

First, create a private-queue context via NSPersistentContainer newBackgroundContext or by setting the concurrency type to NSPrivateQueueConcurrencyType. Second, wrap the fetch and any heavy processing inside perform or performAndWait so Core Data serializes the work onto that context's queue. Third, extract only safe data to pass across the thread boundary, such as NSManagedObjectID instances or plain Swift structs containing primitive values. Fourth, on the main thread, use the main-queue context to fetch by objectID or to refresh existing objects, then update the UI. If you are using parent-child contexts, the background child saves and the parent main context merges the changes, but you still should not pass the live objects across threads.

The mistakes people make

Passing an NSManagedObject instance directly from the background to the main thread is the most common red flag and will crash under concurrency debugging. Another mistake is creating one shared NSManagedObjectContext and accessing it from multiple threads without perform blocks. Some candidates suggest using threadDictionary or dispatching to the main queue inside a background loop, which still violates queue confinement. A less obvious error is fetching on a background context and then calling save without handling merge conflicts or propagating changes to the main context, leaving the UI stale.

What usually comes next

How would you handle a long-running import of thousands of rows? What merge policy do you set on the context? How does parent-child differ from independent contexts with NSPersistentContainer? What happens if you access a relationship on an NSManagedObject that was fetched on a different thread? When would you use perform versus performAndWait, and how do you avoid deadlocks?

A concrete example

Suppose you need to refresh a list of messages. You call the persistentContainer newBackgroundContext method and inside the background context's perform block you execute an NSFetchRequest for Message entities. You map the results to a lightweight MessageViewModel struct containing only strings and objectIDs. You then dispatch to the main queue and pass that array to your view controller. The view controller uses the main context to call existingObject with the objectID if it needs the live object, or simply renders the view models directly. This keeps all Core Data work on the correct queues and avoids threading violations.

Interview question

Which pattern safely updates the UI after fetching data on a background Core Data context?

  • a.Pass the fetched NSManagedObjects to the main queue and access their properties directly in the view controller.
  • b.Use a single shared NSManagedObjectContext and perform the fetch on a global background queue.
  • c.Save the background context and rely on the main context to automatically refresh its registered objects.
  • d.Pass NSManagedObjectID values or lightweight structs to the main queue, then use the main context to fetch by ID.Correct
Why?

NSManagedObjects are tied to their context's queue and are not thread-safe, so you must pass only NSManagedObjectID values or plain structs to the main thread and re-fetch by ID on the main context. Passing live NSManagedObjects to the main queue is unsafe because the objects were instantiated on the background context's queue.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles