Database Reference
In-Depth Information
The managed object model
The
NSManagedObjectModel
represents each object type in your app's data model,
the properties they can have, and the relationship between them. Other parts of
the Core Data stack use the model to create objects, store properties and save
data.
As mentioned earlier in the topic, it can be helpful to think about
NSManagedObjectModel
as a database schema. If your Core Data stack uses SQLite
under the hood, that is absolutely what
NSManagedObjectModel
represents.
However, SQLite is only one of many persistent store types you can use in Core
Data (more on this later), so it's better to think of the managed object model in
more general terms.
Note:
You may be wondering how
NSManagedObjectModel
relates to the data
model editor you've been using all along. Good question!
The visual editor creates and edits an
xcdatamodel
file. There's a special
compiler,
momc
, that compiles the model file into a set of files in a
momd
folder.
Just as your Swift code is compiled and optimized so it can run on a device,
the compiled model can be accessed efficiently at runtime. Core Data uses the
compiled contents of the
momd
folder to initialize an
NSManagedObjectModel
at
runtime.
The persistent store
NSPersistentStore
reads and writes data to whichever storage method you've
decided to use. Core Data provides four types of
NSPersistentStore
out of the box:
three atomic and one non-atomic.
An
atomic
persistent store needs to be completely deserialized and loaded into
memory before you can make any read or write operations. In contrast, a
non-
atomic
persistent store can load chunks of itself onto memory as needed.
Here's a brief overview of the four built-in Core Data store types:
1.
NSQLiteStoreType
is backed by an SQLite database. It is the only non-atomic
store type that Core Data supports out of the box, giving it a lightweight and
efficient memory footprint. This makes it the best choice for most iOS projects.
Xcode's Core Data template uses this store type by default.
2.
NSXMLStoreType
is backed by an XML file, making it the most human-readable
of all the store types. This store type is atomic, so it can have a large memory
footprint.
NSXMLStoreType
is only available on OS X.