您的位置:首页 > 移动开发 > IOS开发

[IOS]Core Data Model--Demo3

2016-04-17 12:11 399 查看

Core Data Model

Principle:

xcdatamodal file 是一种特别的编辑器(compiler)可视性的创建和编辑方式。

Core Data provides four types of NSPersistenceStore out of the box: 3 atomic and 1 non-atomic.

An atomic persistence store needs to be completely deserialized and loaded into memory before you can make any read or write opreations.

(全部加载到内存中才能使用。)

An non-atomic persistence store can load chunks of itself onto memory as needed.

(部分加载,效率更高)

NSQLiteStoreType is backed by an SQLite database. It is the only non-atomic store type that Core Data supports out of the box, giving it a lightweight and efficient memory footprint. This makes it the best choice for most IOS projects. Xcode’s Core data template uses this store type by default.

(Tho other store types are used in MAC OS and so on. When designing in IOS, you just need the NSQLiteStoreType store type.)

The persistent store coordinator

NSPersistentStoreCoordinator is the bridge between the managed object model and the persistent store. It is responsible for using the model and the persistent stores to do most of the hard work in Core Data.

The managed object context

NSManagedObjectContext is the most useful and important components in you daily using.

The basic parts:

A context is an in-memory scratchap for working with your managed objects.

You do all of the work with your Core Data objects within a managed object context.

Any changes you make won’t affect the underlying data on dist until you call save() on the context.

Another five things important:

The context manages the lifecycle of the objects that it creates or fetches. This lifecycle management includes powerful features such as faulting, inverse relationship handling and validation.

A managed objecy cannot exist without an assocaited context. In fact, a maneged object and its context are so tightly coupled that everey manged obejct keeps a reference to its context.

let employeeContext = emplotee.managedObjectContext


A managed object has associated with a particular context, it will remain associated with the same context for the duration of its lifecycle.

An application can use more than one context. You can actually load the same Core Data object onto two different contexts simultaneously.

数据模型

一个dog可能会散步很多次,所以应该有一个数组来存储散步的date。但在core data中没有数组这种类型,而是通过relationships来实现。

relationship里有两种type,如果希望是用数组,那就是to many。

如果希望按时间来排序,就选上order。

同样的每一个walk(date)都对应一条狗的记录。所以应该有walk和dog的relationships。而这时就是to one了。

inverse是指返回逆推。(两者都能互相逆推。)

最后直接导出数据模型的子类文件。

Demo 下载。

[link](http://pan.baidu.com/s/1nv3Yc3Z

(IOS 9可运行。有注释。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: