Swift 学习笔记 Core Data (一)

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


  1. 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.

  2. Select the new untitled property.

    The property settings are displayed in the Relationships or Attributes pane of the Data Model inspector.

  3. 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.

Swift 学习笔记 Core Data (一)_第1张图片

至此你已建entity, 当app启动,这些entities就会用以创建NSManagedObject Instances


Defining an Entity/ 定义一个Entity

你可以在Data Model Inspector中细细把玩这个小东西


Swift 学习笔记 Core Data (一)_第2张图片



Entity Name and Class Name(Subclass of NSManagedObject) are not the same


Abstract Entities

比如人是虚构的,但是前台和开发都是具体的可以实例化


Entity Inheritance

参考类中的继承,

Swift 学习笔记 Core Data (一)_第3张图片


设置Attributes and Relationships




你可能感兴趣的:(Swift 学习笔记 Core Data (一))