Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life-cycle and object graph management, including persistence.
Core Data 是一个你在应用中用来管理模型层对象的框架。 它为关联对象生命周期和对象图管理的常见任务提供了通用的和自动的解决方案,包括持久化。
为什么使用Core Data?
There are a number of reasons why it may be appropriate for you to use Core Data. One of the simplest metrics is that, with Core Data, the amount of code you write to support the model layer of your application is typically 50% to 70% smaller as measured by lines of code. This is primarily due to the features listed above—the features Core Data provides are features you don’t have to implement yourself. Moreover they’re features you don’t have to test yourself, and in particular you don’t have to optimize yourself.
使用 Core Data 之前需要初始化 Core Data Stack, 它会为我们处理所有和底层数据的交互,而我们只需要关注自己的业务逻辑。
The Core Data stack is a collection of framework objects that are accessed as part of the initialization of Core Data and that mediate between the objects in your application and external data stores.
Core Data Stack 示意图:
An external persistent store that contains saved records.
A persistent object store that maps between records in the store and objects in your application.
A persistent store coordinator that aggregates all the stores.
A managed object model that describes the entities in the stores.
A managed object context that provides a scratch pad for managed objects.
A managed object is a model object (in the model-view-controller sense) that represents a record from a persistent store.
Store File: 顾名思义即存储文件,它可以是 SQLite, XML 或 binary 文件;
Persistent Object Store: 它负责把从 Store File 读取数据映射成应用能理解的记录,把应用产生的记录根据配置的存储类型写入到 Store File 中;
Persistent Store Coordinator: 它负责协调有多个 Persistent Object Store 的情况;
Managed Object Model: 它是实体描述的集合,实体类似于数据库中的表;
Managed Object Context: 它为 managed objects 提供暂存的空间,也是应用操作 Core Data 的接口;
Managed Object: 它是代表某个实体的模型对象。
我是这样理解它们的:Store File 是具体存放二进制数据的地方,二进制数据也有组织方式,所以会引入 SQLite, XML 或 binary,直接操作二进制文件肯定不方便,那自然想到用一个对象来做这个事情,于是引入 Persistent Object Store,这样底层二进制的操作对上层来说就透明了。实体在这里就是一个能被双方理解的数据结构,本来它应该被直接被 Persistent Object Store 持有,有了支持多个 Store 的 Persistent Store Coordinator 后,让 coordinator 来持有它,显得更合理。Managed Object Model 就是所有实体描述的集合。Managed Object 是代表某个实体的模型对象,实体有个类名来关联它。
In some respects, an NSManagedObject acts like a dictionary—it is a generic container object that efficiently provides storage for the properties defined by its associated NSEntityDescription object. NSManagedObject provides support for a range of common types for attribute values, including string, date, and number (see NSAttributeDescription for full details). There is therefore commonly no need to define instance variables in subclasses. Sometimes, however, you want to use types that
are not supported directly, such as colors and C structures. For example, in a graphics application you might want to define a Rectangle entity that has attributes color and bounds that are an instance of NSColor and an NSRect struct respectively. For some types you can use a transformable attribute, for others this may require you to create a subclass of NSManagedObject—see Non-Standard Persistent Attributes.