The Core Data Stack: Persistent Store Types

When using Core Data most use SQLite without considering the other options. But what are the advantages and disadvantages of each of the alternatives?

Terminology

  • Atomic: A store that needs to be completely deserialized and loaded into memory before there can be any read or write operations.
  • Non-atomic: Such a persistent store can load chunks of data into memory as needed.

A persistent store is the interface between the coordinator and the permanent state of the object graph for both reading and writing. When a change is pushed onto the store, the change becomes a permanent part of the object state, meaning the position of the storage in memory or on disk is not relevant.

NSInMemoryStoreType: Stores the entire object graph in memory. Persistent, although not in the case that you turn off the machine or shut down the App.

NSBinaryStoreType: Of the atomic storetypes this occupies the least disk space, and loads the fastest. Not compressed. As an atomic store type every Code Data object is loaded at once.

NSSQLiteStoreType: Non-atomic. Occupies binary-like disk space (small), loads quickly but the file needs to be available with exclusive access. Supports atomic updates. Core Data uses this type by default, and many Apps seem to use this as their go-to backing store. Non-atomic, so has a smaller memory footprint than the atomic types.

NSXMLStoreType: Not supported in iOS, but XML is human readable rather than going for outright speed. An atomic type, so can have a large memory footprint.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store