数据存储-- Core Data的使用(二)
一、基础概念深入
1.NSManagedObjectContext
被管理数据上下文就像便笺簿
当从数据持久层获取数据时,相当于把这些临时的数据拷贝写在便笺簿上,然后就可以随心所欲的修改这些值。
通过上下文,可以对数据记录NSManagedObject进行添加删除更改,记录更改后支持撤销和重做。
除非你保存这些数据变化,否则持久层的东西是不会变化。
通常我们将 controller 类或其子类与 Managed Object Context NSManagedObjectContext绑定,这样就方便我们动态地生成,获取数据对象等。
常用的方法:
| -save: | 将数据对象保存到数据文件 | 
| -objectWithID: | 查询指定 Managed Object ID 的数据对象 | 
| -deleteObject: | 将一个数据对象标记为删除,但是要等到 Context 提交更改时才真正删除数据对象 | 
| -undo | 回滚最后一步操作,这是都 undo/redo 的支持 | 
| -lock | 加锁,常用于多线程以及创建事务。同类接口还有:-unlock and -tryLock | 
| -rollback | 还原数据文件内容 | 
| -reset | 清除缓存的 Managed Objects。只应当在添加或删除 Persistent Stores 时使用 | 
| -undoManager | 返回当前 Context 所使用的 NSUndoManager | 
| -assignObject: toPersistantStore: | 由于 Context 可以管理从不同数据文件而来的数据对象, 这个接口的作用就是指定数据对象的存储数据文件(通过指定 PersistantStore 实现) | 
| -executeFetchRequest: error: | 执行获取数据请求,返回所有匹配的数据对象 | 
2.NSManagedObject
被管理的数据记录,相当于数据库中的一条记录
每一个NSManagedObject对象,都有一个全局 ID(类型为:NSManagedObjectID)。每个在NSManagedObjectContext注册过
的NSManagedObject,可以通过这个全局 ID 在上下文中查询到。
每个在持久存储层中的对象,都对应一个与上下文相关的NSManagedObject
常用的方法:
-entity 获取实体
-objectID 获取NSManagedObjectID
-valueForKey: 获取指定 Property 的值
-setValue: forKey: 设定指定 Property 的值
3.NSFetchRequest
获取数据的请求,通过被管理数据的上下文来执行查询,比如
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
查询时,必须指定查询实体或实体名称,以 NSArray 形式返回查询结果,如果我们没有设置任何查询条件,则返回该 Entity 的所有数据对象。
我们可以使用谓词来设置查询条件,通常会将常用的 Fetch Requests 保存到 dictionary 以重复利用。
NSFetchRequest包括以下部分:
(1)实体(Entity)的名称
(2)NSPredicate谓词(搜索关键字或限定条件)
(3)排序方式(NSArray *)sortDescriptors
所有的被管理对象(managed object)都必须在上下文中注册,而通过NSFetchRequest获得的对象自动被注册。
如果在上下文中已经存在了要获取的对象,那么这个被管理NSManagedObject将被返回。否则上下文就会从相关的数据源中查找(也可能找不到)
例如,以下代码是查询在指定日期之后创建的ContactInfo,并将查询结果按照name排序

NSManagedObjectContext * context = [self managedObjectContext];
NSManagedObjectModel * model = [self managedObjectModel];
NSDictionary * entities = [model entitiesByName];
NSEntityDescription * entity = [entities valueForKey:@"ContactInfo"]; NSPredicate * predicate;
predicate = [NSPredicate predicateWithFormat:@"creationDate > %@", date]; NSSortDescriptor * sort = [[NSortDescriptor alloc] initWithKey:@"name"];
NSArray * sortDescriptors = [NSArray arrayWithObject: sort]; NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
[fetch setEntity: entity];
[fetch setPredicate: predicate];
[fetch setSortDescriptors: sortDescriptors]; NSArray * results = [context executeFetchRequest:fetch error:nil];
[sort release];
[fetch release];

常用方法:
| -setEntity: | 设置你要查询的数据对象的类型(Entity) | 
| -setPredicate: | 设置查询条件 | 
| -setFetchLimit: | 设置最大查询对象数目 | 
| -setSortDescriptors: | 设置查询结果的排序方法 | 
| -setAffectedStores: | 设置可以在哪些数据存储中查询 | 
4.NSPersistentStoreCoordinator
持久化数据助理
Core Data定义了一个栈,持久化存储助理在中间,栈顶是被管理数据的上下文,栈底是持久化存储层,结构如图

通常从磁盘上的数据文件中读取或存储数据,这些底层的读写就由它来处理。一般我们无需与它直接打交道,上下文已经封装了对它的调用
常用方法:
| -addPersistentStoreForURL:configuration:URL:options:error: | 加载持久化存储数据,对应的卸载接口为 -removePersistentStore:error: | 
| -migratePersistentStore:toURL:options:withType:error: | 迁移数据存储,效果与 "save as"相似,但是操作成功后, 迁移前的数据存储不可再使用 | 
| -managedObjectIDForURIRepresentation: | 返回给定 URL所指示的数据存储的 object id,如果找不到匹配的数据存储则返回 nil | 
| -persistentStoreForURL: | 返回指定路径的 Persistent Store | 
| -URLForPersistentStore: | 返回指定 Persistent Store 的存储路径 | 
5.NSManagedObjectModel
被管理的数据模型,用来描述程序的实体、其属性、关系的模型图
包括以下几个部分:
(1)实体(Entity)
对应NSEntityDescription对象
相当于数据库中的一个表
实体名称(name)
实体类名:NSManagedObject子类的名称
实体实例:NSManagedObject对象或其子类的实例
NSEntityDescription 常用方法:
+insertNewObjectForEntityForName:inManagedObjectContext: 工厂方法,
根据给定的 Entity 描述,生成相应的 NSManagedObject 对象,并插入 ManagedObjectContext 中。
-managedObjectClassName返回映射到 Entity 的 NSManagedObject 类名
-attributesByName以名字为 key, 返回 Entity 中对应的 Attributes
-relationshipsByName以名字为 key, 返回 Entity 中对应的 Relationships
(2)属性(Property)
对应NSPropertyDescription对象
Property 为 Entity 的特性,它相当于数据库表中的一列,或者 XML 文件中的 value-key 对中的 key。
它可以描述实体基本属性(Attribute),实体之间的关系(RelationShip),或查询属性(Fetched Property)。
<1> 实体的基本属性(Attributes)
对应NSAttributeDescription对象
存储基本数据,数据类型包括:
string,date,integer(NSString, NSDate, NSNumber)
<2> 实体间的关系(Relationships)
对应NSRelationshipDescription对象
支持对一、对多的关系
<3> 查询属性(Fetched Property)
对应NSFetchedPropertyDescription对象
根据查询谓词返回指定实体的符合条件的数据对象
表示了一种“弱”的、单项的关系(相当于数据库中的查询语句)
6.持久化存储层(Persistent Stores)
持久化存储层是和文件或外部数据库关联的,大多数访问持久化存储层的动作都由上下文来完成。
7.NSFetchedResultsController
用于在表视图table view中加载部分数据
二、用代码创建数据模型

NSManagedObjectModel *managedObjectModel()
{
static NSManagedObjectModel *moModel = nil; if (moModel != nil) {
return moModel;
} moModel = [[NSManagedObjectModel alloc] init]; // Create the entity NSEntityDescription *runEntity = [[NSEntityDescription alloc] init];
[runEntity setName:@"Run"];
[runEntity setManagedObjectClassName:@"Run"]; [moModel setEntities:[NSArray arrayWithObject:runEntity]]; // Add the Attributes NSAttributeDescription *dateAttribute = [[NSAttributeDescription alloc] init];
[dateAttribute setName:@"date"];
[dateAttribute setAttributeType:NSDateAttributeType];
[dateAttribute setOptional:NO]; NSAttributeDescription *idAttribute = [[NSAttributeDescription alloc] init];
[idAttribute setName:@"processID"];
[idAttribute setAttributeType:NSInteger32AttributeType];
[idAttribute setOptional:NO];
[idAttribute setDefaultValue:[NSNumber numberWithInteger:-1]]; // Create the validation predicate for the process ID.
// The following code is equivalent to validationPredicate = [NSPredicate predicateWithFormat:@"SELF > 0"] NSExpression *lhs = [NSExpression expressionForEvaluatedObject];
NSExpression *rhs = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:0]]; NSPredicate *validationPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSGreaterThanPredicateOperatorType
options:0]; NSString *validationWarning = @"Process ID < 1";
[idAttribute setValidationPredicates:[NSArray arrayWithObject:validationPredicate]
withValidationWarnings:[NSArray arrayWithObject:validationWarning]]; NSArray *properties = [NSArray arrayWithObjects: dateAttribute, idAttribute, nil];
[runEntity setProperties:properties]; // Add a Localization Dictionary NSMutableDictionary *localizationDictionary = [NSMutableDictionary dictionary];
[localizationDictionary setObject:@"Date" forKey:@"Property/date/Entity/Run"];
[localizationDictionary setObject:@"Process ID" forKey:@"Property/processID/Entity/Run"];
[localizationDictionary setObject:@"Process ID must not be less than 1" forKey:@"ErrorString/Process ID < 1"]; [moModel setLocalizationDictionary:localizationDictionary]; return moModel;
}

1)我们创建了一个全局模型 moModel;
2)并在其中创建一个名为 Run 的 Entity,这个 Entity 对应的 ManagedObject 类名为 Run(很快我们将创建这样一个类);
3)给 Run Entity 添加了两个必须的 Property:date 和 processID,分别表示运行时间以及进程 ID;并设置默认的进程 ID 为 -1;
4)给 processID 特性设置检验条件:必须大于 0;
5)给模型设置本地化描述词典;
本地化描述提供对 Entity,Property,Error信息等的便于理解的描述,其可用的键值对如下表:
| Key | Value | 
| "Entity/NonLocalizedEntityName" | "LocalizedEntityName" | 
| "Property/NonLocalizedPropertyName/Entity/EntityName" | "LocalizedPropertyName" | 
| "Property/NonLocalizedPropertyName" | "LocalizedPropertyName" | 
| "ErrorString/NonLocalizedErrorString" | "LocalizedErrorString" | 
三、存储数据到xml文件
存储类型为NSXMLStoreType

NSManagedObjectContext *managedObjectContext()
{
static NSManagedObjectContext *moContext = nil;
if (moContext != nil) {
return moContext;
} moContext = [[NSManagedObjectContext alloc] init]; // Create a persistent store coordinator, then set the coordinator for the context. NSManagedObjectModel *moModel = managedObjectModel();
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:moModel];
[moContext setPersistentStoreCoordinator: coordinator]; // Create a new persistent store of the appropriate type. NSString *STORE_TYPE = NSXMLStoreType;
NSString *STORE_FILENAME = @"CoreDataTutorial.xml"; NSError *error = nil;
NSURL *url = [applicationDocmentDirectory() URLByAppendingPathComponent:STORE_FILENAME]; NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE
configuration:nil
URL:url
options:nil
error:&error]; if (newStore == nil) {
NSLog(@"Store Configuration Failure\n%@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
} return moContext;
}

四、自定义NSManagedObject子类
比如,Run.h文件

#import <CoreData/NSManagedObject.h> @interface Run : NSManagedObject
{
NSInteger processID;
} @property (retain) NSDate *date;
@property (retain) NSDate *primitiveDate;
@property NSInteger processID; @end

Run.m文件

#import "Run.h" @implementation Run @dynamic date;
@dynamic primitiveDate; - (void) awakeFromInsert
{
[super awakeFromInsert]; self.primitiveDate = [NSDate date];
} #pragma mark -
#pragma mark Getter and setter - (NSInteger)processID
{
[self willAccessValueForKey:@"processID"];
NSInteger pid = processID;
[self didAccessValueForKey:@"processID"];
return pid;
} - (void)setProcessID:(NSInteger)newProcessID
{
[self willChangeValueForKey:@"processID"];
processID = newProcessID;
[self didChangeValueForKey:@"processID"];
} // Implement a setNilValueForKey: method. If the key is “processID” then set processID to 0. - (void)setNilValueForKey:(NSString *)key { if ([key isEqualToString:@"processID"]) {
self.processID = 0;
}
else {
[super setNilValueForKey:key];
}
} @end

1)这个类中的 date 和 primitiveDate 的访问属性为 @dynamic,这表明在运行期会动态生成对应的 setter 和 getter;
2)在这里我们演示了如何正确地手动实现 processID 的 setter 和 getter:为了让 ManagedObjecContext  能够检测 processID的变化,以及自动支持 undo/redo,我们需要在访问和更改数据对象时告之系统,will/didAccessValueForKey 以及 will/didChangeValueForKey 就是起这个作用的。
3)当我们设置 nil 给数据对象 processID 时,我们可以在 setNilValueForKey 捕获这个情况,并将 processID  置 0;
4)当数据对象被插入到 ManagedObjectContext 时,我们在 awakeFromInsert 将时间设置为当前时间。
数据存储-- Core Data的使用(二)的更多相关文章
- iOS数据持久化 -- Core Data-备用
		Core Data是一个功能强大的层,位于SQLite数据库之上,它避免了SQL的复杂性,能让我们以更自然的方式与数据库进行交互.Core Data将数据库行转换为OC对象(托管对象)来实现,这样无需 ... 
- iOS数据持久化 -- Core Data
		Core Data是一个功能强大的层,位于SQLite数据库之上,它避免了SQL的复杂性,能让我们以更自然的方式与数据库进行交互.Core Data将数据库行转换为OC对象(托管对象)来实现,这样无需 ... 
- 元素的数据存储-jQuery.data()与.data()
		jQuery提供的存储接口 jQuery.data( element, key, value ) //静态接口,存数据 jQuery.data( element, key ) //静态接口,取数据 . ... 
- Android应用开发基础之二:数据存储和界面展现(二)
		常见布局 相对布局 RelativeLayout 组件默认左对齐.顶部对齐 设置组件在指定组件的右边 android:layout_toRightOf="@id/tv1" 设置在指 ... 
- iOS开发 - OC - 实现本地数据存储的几种方式二(直接使用sqlite)
		连接上一篇文章http://www.cnblogs.com/FBiOSBlog/p/5819418.html. 上一篇文章介绍了OC内部一些方法进行数据的本地存储,其中包括 NSUser类.Plist ... 
- HTML5数据存储方案data与jQuery数据存储方案$.data()的区别
		我们先看下$.fn.data()的使用,这个和$.data()是不一样的,前者是和某个jquery对象相关,后者则是全局方法.主要有data()和removeData()这2个实例方法.通过下面的例子 ... 
- Core Data的使用(二)备
		一.基础概念深入 1.NSManagedObjectContext 被管理数据上下文就像便笺簿 当从数据持久层获取数据时,相当于把这些临时的数据拷贝写在便笺簿上,然后就可以随心所欲的修改这些值. 通过 ... 
- 《驾驭Core Data》 第三章 数据建模
		本文由海水的味道编译整理,请勿转载,请勿用于商业用途. 当前版本号:0.1.2 第三章数据建模 Core Data栈配置好之后,接下来的工作就是设计对象图,在Core Data框架中,对象图被表 ... 
- iOS 数据持久化(3):Core Data
		@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ... 
随机推荐
- .NET  BETWEEN方法
			Between 值范围比较 可以判断一个值是否落在区间范围值中. public static bool Between<T>(this T me, T lower, T upper) wh ... 
- vmware 虚拟机三种网卡
			转:https://blog.csdn.net/lyf_ldh/article/details/78695357 vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NA ... 
- Web前端学习笔记之jQuery基础
			0x0 jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Aja ... 
- react-refetch的使用小例子
			出处:<react设计模式和最佳实践> 作者:米凯莱·贝尔托利 出版时间:2018年8月第1版(还算新) 使用react-refetch来简化api获取数据的代码 const List = ... 
- [Linux 004]——用户和用户组以及 Linux 权限管理(二)
			到权限了.前面讲到了 Linux 中的用户和用户主管理,其实它们的本质(或者用户和用户组出现的初衷)都是方便权限管理.权限管理对于计算机的重要性不言而喻,权限让每个用户能够安安心心的使用计算机,而不用 ... 
- 20145324 《Java程序设计》第9周学习总结
			20145324 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API中的接口会有数据库厂商操作 ... 
- Windows下如何安装python第三方库lxml
			lxml是个非常有用的python库,它可以灵活高效地解析xml,与BeautifulSoup.requests结合,是编写爬虫的标准姿势. 参考 Windows下如何安装python第三方库lxml ... 
- linux信号的介绍
			1.基本概念 中断: 中断是系统对于异步事件的响应 中断信号 中断源 现场信息 中断处理程序 中断向量表 ... 
- matlab练习程序(求灰度图像最大灰度,最小灰度,平均灰度)
			转自 http://www.cnblogs.com/tiandsp/archive/2012/01/07/2316006.html 感谢Dsp tian clearclc;img = imread( ... 
- openwrt中如何在一个软件包中使能busybox中的工具
			答:在软件包的Makefile中定义一个宏Package/package-name/config 举例:笔者自己制作了一个名为hello的软件包,但是这个软件包依赖busybox中的ifdown de ... 
