coredata学习总结(一)

来源:互联网 发布:matlab迭代算法程序 编辑:程序博客网 时间:2024/05/23 15:58

What Is Core Data?

Core data是一种应用程序管理model层对象的框架。他提供了和对象生命周期和对象图形管理以及持久化有关的通用和自动的解决方案

Creating a Managed Object Model

core data的大部分功能都来源于你为其设定的实体,实体属性,和实体之间的关系。core data使用名为managed object model的模式-NSManagedObjectModel的实例。总体来说,这个model越丰富,core data能支持的功能就越好。

一个managed object model允许core data来将持久化存储数据映射为应用程序中使用的managed objects.这个model是一系列的NSEntityDescription实体描述对象集合。一个entity description按照实体的名字来描述了一个实体(你可以想象成数据库中的一张表)。类名用来代表应用程序中的实体,和其拥有的属性和关系。

Creating an Entity and Its Properties

创建项目时勾选Use Core Data选项,那么就会创建Core Data model的源文件作为模版的一部分。这个源文件后缀为.xcdatamodeld。选择后即可看到Core Data model编辑器。

To create an entity

  1. 点击Add Entity。

    一个未命名的新的entity就会出现在实体列表中。

  2. 选择这个新创建的entity。

  3. 输入entity名称保存返回。

To create attributes and relationships for the entity

  1. 选择新创建的实体,长点击底部的加号。

    一个新的未命名属性或者关系就可以通过弹出的相应选项来添加上了。

  2. 选择新的未命名属性。

  3. 给予属性名称并点击return。

Figure 2-2 展示了一个名为Employee的实体,和用来描述Employee的属性:date of birth, name, and start date.

  Figure 2-1Employee entity in the Xcode Data Model editor


https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/Art/Model_Editor_2x.png

现在你在model中新创建了一个entity,但是没有创建任何的数据。数据会在你launch应用程序的时候被创建。在你的应用程序中这些实体会被用作创建managed objectsNSManagedObject实例的基础。

Defining an Entity

现在你命名了新创建的实体,你还要爱data model inspector中进一步定义他。

Figure 2-2Entity pane in the Data Model inspectorimage: ../Art/Entity_Inspector_2x.png

Entity Name and Class Name

注意entity名字和类名(NSManagedObject子类)是不一样的。在data model中的entity结构不需要来匹配类的层级。

Figure 2-2 展示了objective-c风格的MO为后缀的类名。entity名字和类名是必须的。

Abstract Entities

抽象实体。如果你不会实例划一个entity,那么就要把他指定为抽象的实体。如果你有多个实体都共有一个entity特性,那么就应该创建一个抽象类的实体而不是去实例划他。例如,在Employee实体中你可以将Person定义为抽象实体并且指明只有具体的几个子实体(Employee and Customer)可以被实例划。你可以直接设置某个entity为抽象实体。

Entity Inheritance

如果有多个实体非常相似,那么久可以把公共的部分抽取出来作为其父entity。

Note

Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity will exist within the same table in SQLite. This factor in the design of the SQLite persistent store can create a performance issue.

Figure 2-3Entity inheritance diagramimage: ../Art/Entity_Inheritence_2_2x.png

Defining Attributes and Relationships

一个实体的属性包含他的属性和关系,包括他的fetched请求如果有的话。属性一般都有名字和类型。属性名字定义要注意保留字,不能是任何nsobjec方法名称或者NSManagedObject方法名称。

transient属性是你给model定义的属性但是并不会做为entity实例数据的一部分作持久化处理。core data会跟踪临时属性的改变。使用临时属性的目的包括保留计算值和派生值。


Note

If you undo a change to a transient property that uses nonmodeled information, Core Data does not invoke your set accessor with the old value — it simply updates the snapshot information.

Figure 2-4Attribute pane in the Data Model inspectorimage: ../Art/Attribute_Inspector_2x.png

Attributes

optional意思是不需要有值,但是并不推荐这么用。就是说定义属性的时候要给值。


Relationships and Fetched Properties

To define a relationship, select it in the Core Data model editor, and specify values in the Relationship pane of the Data Model inspector.

Figure 2-5Relationship in the Data Model inspectorimage: ../Art/Relationship_Inspector_2x.png

Relationships are described in greater detail in Creating Managed Object Relationships.

core data支持对一和对多的关系以及fetched属性。fetched属性代表weak,one-way关系。在employeesdepartments链中,一个fetched属性可能是“recent hires”。

type选项定义了关系是否是to-one或者to-many类型。如果想定义一个many-to-many的关系,你需要创建两个to-many关系然后将他们互相设置为inverses。

destination选项定义了当关系在代码中执行时,返回什么对象或者对象集合。如果一个关系定义为to-one的话那么将返回一个对象。如果关系被定义为to-many,那么返回的可能就是一个set集合。这两种方式都有可能返回nil,如果定义的时候properties是optional的话。

inverse选项提供了创建多个关系的方式。通常情况下创建关系都是单向的,这个选项可以将两个关系绑定一起来作为一个完整的关系。


0 0