Core Data是一个管理model层的框架 (备注:Core Data已被Realm吊打)
Core Data 框架提供以下特殊服务:
- 在基本文本处理上提供tracking和内置的undo , redo功能
- change管理,维护object 间关系一致性
- Object的lazy loading,
- 自动 property value validation
- schema migration tool
- Optional integration with the application’s controller layer to support user interface synchronization.
- Grouping, filtering, and organizing data in memory and in the user interface.
- Automatic support for storing objects in external data repositories.
- Sophisticated query compilation. Instead of writing SQL, you can create complex queries by associating an NSPredicate object with a fetch request.
- Version tracking and optimistic locking to support automatic multiwriter conflict resolution.
- Effective integration with the OS X and iOS tool chains.
Creating a Managed Object Model
Core Data 使用的schema是一个NSManagedObjectModel.
detity description objects 是 NSEntityDescription,就像数据库的table
创建Entity and Properties
创建xcode项目, 勾选Use Core Data. 一个source file .xcdatamodeld被创建
. 在navigator 区选中
创建entity:
1. Click Add Entity
2. Select 没命名的Entity
3. 在Data Model inspector中填入名称
为entity创建attributes和relationships
-
With the new entity selected, click the plus(+) sign at the bottom of the appropriate section.
A new untitled attribute or relationship (generically referred to as properties) is added under the Attributes or Relationships section of the editor area.
-
Select the new untitled property.
The property settings are displayed in the Relationships or Attributes pane of the Data Model inspector.
-
Give the property a name and press Return.
The attribute or relationship information appears in the editor area.
Figure 2-2 shows an entity called Employee, with attributes that describe the employee: date of birth, name, and start date.
![image: ../Art/Model_Editor_2x.png](https://i-blog.csdnimg.cn/blog_migrate/6a1753fd5192c9f9a0b3e2e94b838239.png)
Entity Name and Class Name(Subclass of NSManagedObject) are not the same
Abstract Entities
设置Attributes and Relationships