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.

image: ../Art/Model_Editor_2x.png

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


Defining an Entity/ 定义一个Entity

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


image: ../Art/Entity_Inspector_2x.png



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


Abstract Entities

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


Entity Inheritance

参考类中的继承,

image: ../Art/Entity_Inheritence_2_2x.png


设置Attributes and Relationships




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值