Iphone数据存储Core Data的简单使用

一、概念

1.Core Data 是数据持久化存储的最佳方式

2.数据最终的存储类型可以是:SQLite数据库,XML,二进制,内存里,或自定义数据类型

Mac OS X 10.5Leopard及以后的版本中,开发者也可以通过继承NSPersistentStore类以创建自定义的存储格式

3.好处:能够合理管理内存,避免使用sql的麻烦,高效

4.构成:

(1)NSManagedObjectContext(被管理的数据上下文)
这个类是一个用户对persistentStore操作的网关,他维护了用户创建或者加载的managed objects。他记录了用户对managed objects的所有改变,以便用来undo或redo,另外当用户要存储现在的managed objects到persistentstore时,只需调用managedObjectsContext的save方法就行了。 
每个应用至少需要一个context,当然可以同时存在多个context,比如多线程时,如NSOperationQueue。context并不是线程安全的,因此在这种情况中用户要自己做好安全工作。

操作实际内容(操作持久层)

作用:插入数据,查询数据,删除数据

(2)NSManagedObjectModel(被管理的数据模型)

数据库所有表格或数据结构,包含各实体的定义信息

作用:添加实体的属性,建立属性之间的关系

操作方法:视图编辑器,或代码

(3)NSPersistentStoreCoordinator(持久化存储助理)
这个类用来控制对PersistentStore的访问。PersistentStoreCoordinator提供了一些列的高级调用供其他类来使用,对PersistentStore进行读和写。

相当于数据库的连接器

作用:设置数据存储的名字,位置,存储方式,和存储时机

(4)NSManagedObject(被管理的数据记录)
Entity定义了数据的结构,但他并不是数据,真正的数据实例是NSManagedObject类或他的子类。
NSManagedObject类支持Key-Value 编码(KVC),像NSDictionary差不多。NSManagedObject提供了valueForKey:和setValue:forKey:用来设置和查询的方法。另外他也提供了对关系操作的方法。

相当于数据库中的表格记录

(5)NSFetchRequest(获取数据的请求)

相当于查询语句

(6)NSEntityDescription(实体结构)

相当于表格结构(获取实体,数据请求对象)
(7)PersistentStore
这个是数据真正存储的地方,CodeData提供了两种存储的选择,分别是sqlite和二进制文件。PersistentStore本身并不是objc类,仅仅是数据存储。 

(8)后缀为.xcdatamodeld的包

里面是.xcdatamodel文件,用数据模型编辑器编辑

编译后为.momd或.mom文件

5.依赖关系

二、基于SQLite数据库时,Core Data的简单使用

和SQLite的区别:只能取出整个实体记录,然后分解,之后才能得到实体的某个属性

1.构建流程

包括:创建数据上下文,创建数据模型,创建数据持久化存储助理

(1)若是新建的工程,选择空白应用程序,next

勾选Use Core Data选项

此时生成的工程文件AppDelegate中,会自动生成被管理的数据上下文等相关代码

(2)比如AppDelegate.h文件中,自动生成

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;

@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

 

- (void)saveContext;

- (NSURL *)applicationDocumentsDirectory;

方法saveContext表示:保存数据到持久层(数据库)

方法applicationDocumentsDirectory表示:应用程序沙箱下的Documents目录路径

(例如/var/mobile/Applications/5FG80A45-DFB5-4087-A1B1-41342A977E21/Documents/)

(3)比如AppDelegate.h文件中,自动生成

@synthesize managedObjectContext = __managedObjectContext;

@synthesize managedObjectModel = __managedObjectModel;

@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

保存数据到持久层

- (void)applicationWillTerminate:(UIApplication *)application

{

    [self saveContext];

}

- (void)saveContext

{

    NSError *error = nil;

    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;

    if (managedObjectContext != nil) {

        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {

            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

            abort();

        } 

    }

}

Documents目录路径

- (NSURL *)applicationDocumentsDirectory

{

    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

}

被管理的数据上下文

初始化的后,必须设置持久化存储助理

- (NSManagedObjectContext *)managedObjectContext

{

    if (__managedObjectContext != nil) {

        return __managedObjectContext;

    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

    if (coordinator != nil) {

        __managedObjectContext = [[NSManagedObjectContext alloc] init];

        [__managedObjectContext setPersistentStoreCoordinator:coordinator];

    }

    return __managedObjectContext;

}

 

被管理的数据模型

初始化必须依赖.momd文件路径,而.momd文件由.xcdatamodeld文件编译而来

- (NSManagedObjectModel *)managedObjectModel

{

    if (__managedObjectModel != nil) {

        return __managedObjectModel;

    }

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TestApp" withExtension:@"momd"];

    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    return __managedObjectModel;

}

持久化存储助理

初始化必须依赖NSManagedObjectModel,之后要指定持久化存储的数据类型,默认的是NSSQLiteStoreType,即SQLite数据库;并指定存储路径为Documents目录下,以及数据库名称

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

{

    if (__persistentStoreCoordinator != nil) {

        return __persistentStoreCoordinator;

    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestApp.sqlite"];

    NSError *error = nil;

    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        abort();

    }   

    return __persistentStoreCoordinator;

}

如果不是新工程,也可以自己写入相关代码

(4)此外还生成了TestApp.xcdatamodeld文件

(5)还自动链接了CoreData.framework

(6)在预编译头.pch文件中,加入导入了CoreData.h头文件

#import <CoreData/CoreData.h>

 

2.创建数据模型(数据模型编辑器操作)

(1)创建实体

选中.xcodedatamodel对象

在右边的数据模型编辑器的底部工具栏点击Add Entity添加实体

在最右侧栏对实体命名

(2)创建实体属性

选中实体,点击底部工具栏的Add Attribute添加属性

选中新添加的属性,对属性进行命名,并设置属性的数据类型Attribute Type

(3)为两个实体建立关系

选中一个实体,在底部工具栏点击Add Relationship添加关系

选中新关系,对关系添加名称,目标destination设置为另个实体

(4)建立返回关系

(当你建立一个目标关系,最好建立一个返回关系)

在另一个实体中建立一个关系并命名,设置目标对象为这之前的实体

并在Inverse属性选这之前的关系名称

 

(5)设置两个关系的删除规则Delete Rule,都为关联模式

关联模式cascade:其中一个数据被删除,另一个实体中的数据也会跟着删除

(6)最终两个对象的关系图为

切换Editor Stype按钮

会看到另一种编辑方式:

 3.插入数据

在AppDelegate.m的application:didFinishLaunchingWithOptions:方法里,调用自定义方法

insertCoreData插入数据,代码如下:

- (void)insertCoreData

{

    NSManagedObjectContext *context = [self managedObjectContext];

   

    NSManagedObject *contactInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactInfo"inManagedObjectContext:context];

    [contactInfo setValue:@"name B" forKey:@"name"];

    [contactInfo setValue:@"birthday B" forKey:@"birthday"];

    [contactInfo setValue:@"age B" forKey:@"age"];

   

    NSManagedObject *contactDetailInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactDetailInfo" inManagedObjectContext:context];

    [contactDetailInfo setValue:@"address B" forKey:@"address"];

    [contactDetailInfo setValue:@"name B" forKey:@"name"];

    [contactDetailInfo setValue:@"telephone B" forKey:@"telephone"];

   

    [contactDetailInfo setValue:contactInfo forKey:@"info"];

    [contactInfo setValue:contactDetailInfo forKey:@"details"];

   

    NSError *error;

    if(![context save:&error])

    {

        NSLog(@"不能保存:%@",[error localizedDescription]);

    }

}

创建数据上下文,调用insertNewObjectForName方法,创建两个数据记录NSManagedObject,然后就可以对之前数据模型编辑视图中定义的属性进行赋值。此时的数据只在内存中被修改,最后调用数据上下文的save方法,保存到持久层

4.查询数据

在调用了insertCoreData之后,可以调用自定的查询方法dataFetchRequest来查询插入的数据

- (void)dataFetchRequest

{

    NSManagedObjectContext *context = [self managedObjectContext];

    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ContactInfo"inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

    NSError *error;

    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

    for (NSManagedObject *info in fetchedObjects) {

        NSLog(@"name:%@", [info valueForKey:@"name"]);

        NSLog(@"age:%@", [info valueForKey:@"age"]);

        NSLog(@"birthday:%@", [info valueForKey:@"birthday"]);

        NSManagedObject *details = [info valueForKey:@"details"];

        NSLog(@"address:%@", [details valueForKey:@"address"]);

         NSLog(@"telephone:%@", [details valueForKey:@"telephone"]);

    }

}

fetchRequest相当于sql查询语句的包装类,需要用setEntity方法,来指定具体查询的实体结构(表结构)

通过NSEntityDescription的entityForName方法来,返回指向该具体实体结构的指针

然后调用executeFetchRequest:error:方法,来执行查询操作,如果操作成功,则返回对应的数据记录数组

其中,可以通过NSManagedObject数据记录对象里关联的属性,查询另一个数据记录对象里的属性

5.数据模版

为每个实体生成一个NSManagedObject子类

上面设置数据和获取数据时,使用的是Key-Value方式,更好的方法是通过生成强类型的NSManagedObject的子类,通过类的成员属性来访问和获取数据

(1)在数据编辑器视图中选中实体对象,

选则file菜单,点击new,点击file...,选择Core Data项,选择NSManagedObject subclass,生成该实体同名的类,

继承于NSManagedObject

生成对应的ContactInfo.h文件

#import <Foundation/Foundation.h>

#import <CoreData/CoreData.h>

@class ContactDetailInfo;

@interface ContactInfo : NSManagedObject

@property (nonatomic, retain) NSString * age;

@property (nonatomic, retain) NSString * birthday;

@property (nonatomic, retain) NSString * name;

@property (nonatomic, retain) ContactDetailInfo *details;

@end

和ContactInfo.m文件

其中,@dynamic告诉编译器不做处理,使编译通过,其getter和setter方法会在运行时动态创建,由Core Data框架为此类属性生成存取方法

#import "ContactInfo.h"

#import "ContactDetailInfo.h"

 

@implementation ContactInfo

 

@dynamic age;

@dynamic birthday;

@dynamic name;

@dynamic details;

 

@end

以及ContactDetailInfo.h文件

#import <Foundation/Foundation.h>

#import <CoreData/CoreData.h>

 

@class ContactInfo;

@interface ContactDetailInfo : NSManagedObject

@property (nonatomic, retain) NSString * address;

@property (nonatomic, retain) NSString * name;

@property (nonatomic, retain) NSString * telephone;

@property (nonatomic, retain) ContactInfo *info;

@end

和ContactDetailInfo.m文件

#import "ContactDetailInfo.h"

#import "ContactInfo.h"

@implementation ContactDetailInfo

 

@dynamic address;

@dynamic name;

@dynamic telephone;

@dynamic info;

@end

此时,数据模型编辑器视图最右边栏中,实体的class就变成具体的类名

之前用Key-Value的代码就可以修改为:

#import "ContactInfo.h"

#import "ContactDetailInfo.h"

- (void)insertCoreData

{

    NSManagedObjectContext *context = [self managedObjectContext];

    ContactInfo *contactInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactInfo"inManagedObjectContext:context];

    contactInfo.name @"name B";

    contactInfo.birthday @"birthday B";

    contactInfo.age @"age B";

   

    ContactDetailInfo *contactDetailInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactDetailInfo" inManagedObjectContext:context];

    contactDetailInfo.address @"address B";

    contactDetailInfo.name @"name B";

    contactDetailInfo.telephone @"telephone B";

    contactDetailInfo.info = contactInfo;

    contactInfo.details = contactDetailInfo;

    NSError *error;

    if(![context save:&error])

    {

        NSLog(@"不能保存:%@",[error localizedDescription]);

    }

}

- (void)dataFetchRequest

{

    NSManagedObjectContext *context = [self managedObjectContext];

    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ContactInfo"inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

    NSError *error;

    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

    for (ContactInfo *info in fetchedObjects) {  

        NSLog(@"name:%@", info.name);

        NSLog(@"age:%@", info.age);

        NSLog(@"birthday:%@", info.birthday);

        ContactDetailInfo *details = info.details;

        NSLog(@"address:%@", details.address);

         NSLog(@"telephone:%@", details.telephone);

    }

}

三、数据库相关

1.打印隐藏的sql语句:

在Edit Scheme中选择Run,之后进入Arguments标签,添加参数:“-com.apple.CoreData.SQLDebug 1”

2.使用SQLite存储时,数据库结构

存储的SQLite数据库表名称:大写“Z”加上实体名称大写,一个实体相当于一张表

具体的字段名称:大写“Z”加上实体属性名称大写

 

二、基础概念深入

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信息等的便于理解的描述,其可用的键值对如下表:

KeyValue
"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 将时间设置为当前时间。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值