What is 'Core Data' and how do you optimize its performance in an iOS application?

Instruction: Describe 'Core Data' and provide strategies for enhancing its performance.

Context: This question assesses the candidate's experience with 'Core Data' for persistence in iOS apps, focusing on performance optimization techniques such as batch processing and fetch requests.

Updated

Official answer

Core Data manages an object graph, change tracking and optional persistence. A SQLite store is common, but Core Data is not simply a thin SQL wrapper. Understand the model, relationships, store type and access patterns before optimizing.

Start with measured fetches. Use selective predicates, appropriate indexes and only the data needed. A sort descriptor orders results; it does not reduce their count. fetchLimit bounds the result count. fetchBatchSize controls incremental materialization: the fetch still evaluates the request and records matching identities, so it is not an overall result limit or automatic pagination. Faults and prefetching can trade memory for extra round trips; inspect actual access behavior.

Use a main-queue context for UI work and private-queue contexts for appropriate imports or processing. Access a context and its objects through its designated queue, using perform where required. Pass object IDs or immutable values across contexts, not managed object instances. Child-context save pushes changes to its parent; persistence still requires the relevant parent/store save. Separate contexts need a deliberate merge and conflict policy.

For supported stores, batch updates/deletes can avoid loading every object, but they bypass normal in-memory change processing. SQLite batch deletes require reconciling affected object IDs or persistent history with active contexts. Check model validation and delete-rule restrictions; faster SQL-level work is not permission to bypass business invariants.

Test bounded imports, cancellation, conflicting edits, deletion of displayed records, memory pressure and recovery from save failures. Measure request latency, faults, retained objects and UI hitches with realistic data. Neither indexes nor background contexts guarantee faster behavior on every workload.

Core Data queue confinement, Fetch batch semantics, Batch deletion and merges

Related Questions