右键新建文件选择CoreData中的CoreDataModel,在.xcdatamodeld文件中添加实体类,并为其添加属性,在Xcode右侧Entity属性栏里的class为实体管理对象起名字,之后右键新建文件选择CoreData下的NSManagedObject subclass自动生成类。之后新建工程勾选CoreData会在AppDelegate中生成代码,将其拷贝,新建文件CoreDataDAO粘贴进去,此类为所有数据访问对象的父类,自己新建数据访问对象可以定义增删改查得方法,另外自己也要定义一个不被管理的实体对象,属性与ManagedObject属性一致,此时文件有.xcdatamodeld文件ManagedObject文件CoreDataDAO文件和自己定义的实体文件共5个,在CoreDataDAO.m文件中修改
- (
NSManagedObjectModel
*)managedObjectModel方法中的
[[
NSBundle
mainBundle
]
URLForResource
:
@"Note"
withExtension
:
@"momd
”
]的名字与.xcdatamodeld一致,
- (
NSPersistentStoreCoordinator
*)persistentStoreCoordinator 中修改
NSURL
*storeURL = [[
self
applicationDocumentsDirectory
]
URLByAppendingPathComponent
:
@"Note.sqlite
”
]行中为数据存储为数据库文件的名字。此时基本工作已经做好。
在子类化的数据访问对象里添加查找方法(无条件查询)
- (
NSMutableArray
*)findAll{
获取上下文对象
NSManagedObjectContext
*cxt = [
self
managedObjectContext
];
获取实体关联描述类
NSEntityDescription
*entityDescription = [
NSEntityDescription
entityForName
:
@"Note"
inManagedObjectContext
:cxt];
数据提取请求类用于查找
NSFetchRequest
*request = [[
NSFetchRequest
alloc
]
init
];
[requestsetEntity:entityDescription];
排序描述类可以指定排序字段及排序方式
NSSortDescriptor
*sortDescriptor = [[
NSSortDescriptor
alloc
]
initWithKey
:
@"name"
ascending
:
YES
];
[request setSortDescriptors : @[ sortDescriptor ] ];
[request setSortDescriptors : @[ sortDescriptor ] ];
NSError *err = nil;
根据前面设置的请求对象查询返回数组
NSArray
*listData = [cxt
executeFetchRequest
:request
error
:&err];
NSMutableArray *resListData = [[NSMutableArrayalloc]init];
遍历数组中的MO对象并将MO对象中的内容赋值于自己定义的实体类中
for
(
NoteManagedObject
*mo
in
listData) {
Note *note = [[ Note alloc ] init ];
note. name = mo. name ;
note. age = [mo. age intValue ];
note. sex = mo. sex ;
[resListData addObject :note];
}
return resListData;
Note *note = [[ Note alloc ] init ];
note. name = mo. name ;
note. age = [mo. age intValue ];
note. sex = mo. sex ;
[resListData addObject :note];
}
return resListData;
}
有条件查询
此方法可以返回数组
- (
Note
*)findById:(
Note
*)model{
NSManagedObjectContext *cxt = [ self managedObjectContext ];
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
NSManagedObjectContext *cxt = [ self managedObjectContext ];
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
[requestsetEntity:entityDescription];
通过谓词限定查询条件
NSPredicate
*predicate = [
NSPredicate
predicateWithFormat
:
@"name = %@"
,model.
name
];
[request setPredicate :predicate];
[request setPredicate :predicate];
NSError *err = nil;
查询返回数组
NSArray
*listData = [cxt
executeFetchRequest
:request
error
:&err];
// NSMutableArray *resListData = [[NSMutableArray alloc]init];
// for (NoteManagedObject *mo in listData) {
// Note *note = [[Note alloc]init];
// note.name = mo.name;
// note.age = [mo.age intValue];
// note.sex = mo.sex;
// [resListData addObject:note];
// }
if (listData. count > 0 ) {
NoteManagedObject *no = [listData lastObject ];
Note *note = [[ Note alloc ] init ];
note. name = no. name ;
note. age = [no. age intValue ];
note. sex = no. sex ;
return note;
}
return nil ;
// NSMutableArray *resListData = [[NSMutableArray alloc]init];
// for (NoteManagedObject *mo in listData) {
// Note *note = [[Note alloc]init];
// note.name = mo.name;
// note.age = [mo.age intValue];
// note.sex = mo.sex;
// [resListData addObject:note];
// }
if (listData. count > 0 ) {
NoteManagedObject *no = [listData lastObject ];
Note *note = [[ Note alloc ] init ];
note. name = no. name ;
note. age = [no. age intValue ];
note. sex = no. sex ;
return note;
}
return nil ;
}
修改数据
插入
- (
int
)create:(
Note
*)model{
NSManagedObjectContext *cxt = [self managedObjectContext];
创建一个被管理的实体对象用于保存
NoteManagedObject
*note = [
NSEntityDescription
insertNewObjectForEntityForName
:
@"Note"
inManagedObjectContext
:cxt];
[note setValue :model. name forKey : @"name" ];
[note setValue :[ NSNumber numberWithInteger :model. age ] forKey : @"age" ];
note. sex = model. sex ;
[note setValue :model. name forKey : @"name" ];
[note setValue :[ NSNumber numberWithInteger :model. age ] forKey : @"age" ];
note. sex = model. sex ;
NSError *error;
语句保存修改同步到数据库中
if
([
self
.
managedObjectContext
save
:&error]) {
NSLog ( @" 数据库保存成功! " );
} else {
NSLog ( @" 数据插入失败! " );
return - 1 ;
}
return 0 ;
NSLog ( @" 数据库保存成功! " );
} else {
NSLog ( @" 数据插入失败! " );
return - 1 ;
}
return 0 ;
}
删除
- (
int
)remove:(
Note
*)model{
NSManagedObjectContext *cxt = [ self managedObjectContext ];
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
[request setEntity :entityDescription];
NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"name = %@" ,model. name ];
[request setPredicate :predicate];
NSError *error;
NSArray *listData = [cxt executeFetchRequest :request error :&error];
if (listData. count > 0 ) {
NSManagedObjectContext *cxt = [ self managedObjectContext ];
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
[request setEntity :entityDescription];
NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"name = %@" ,model. name ];
[request setPredicate :predicate];
NSError *error;
NSArray *listData = [cxt executeFetchRequest :request error :&error];
if (listData. count > 0 ) {
NoteManagedObject *note = [listDatalastObject];
上下文对象调用deleteObject方法删除数据
[
self
.
managedObjectContext
deleteObject
:note];
NSError *savingError =nil;
删除后调用保存方法
if
([
self
.
managedObjectContext
save
:&savingError]) {
NSLog ( @" 删除数据成功! " );
} else {
NSLog ( @" 删除数据失败! " );
return - 1 ;
}
}
return 0 ;
NSLog ( @" 删除数据成功! " );
} else {
NSLog ( @" 删除数据失败! " );
return - 1 ;
}
}
return 0 ;
}
修改
- (
int
)modify:(
Note
*)model{
NSManagedObjectContext *cxt = [ self managedObjectContext ];
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
[request setEntity :entityDescription];
NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"name = %@" ,model. name ];
[request setPredicate :predicate];
NSError *error;
NSArray *listData = [cxt executeFetchRequest :request error :&error];
NSManagedObjectContext *cxt = [ self managedObjectContext ];
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
[request setEntity :entityDescription];
NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"name = %@" ,model. name ];
[request setPredicate :predicate];
NSError *error;
NSArray *listData = [cxt executeFetchRequest :request error :&error];
if (listData.count>0) {
修改对象的内容
NoteManagedObject
*mo = [listData
lastObject
];
mo. name = model. name ;
mo. age = [ NSNumber numberWithInt :model. age ];
mo. sex = model. sex ;
mo. name = model. name ;
mo. age = [ NSNumber numberWithInt :model. age ];
mo. sex = model. sex ;
NSError *savingError =nil;
修改后保存
if
([
self
.
managedObjectContext
save
:&savingError]) {
NSLog ( @" 修改数据成功! " );
} else {
NSLog ( @" 修改数据失败! " );
return - 1 ;
}
}
return 0 ;
NSLog ( @" 修改数据成功! " );
} else {
NSLog ( @" 修改数据失败! " );
return - 1 ;
}
}
return 0 ;
}