简介
Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象。在此数据操作期间,我们不需要编写任何SQL语句,这个有点类似于著名的Hibernate持久化框架,不过功能肯定是没有Hibernate强大的。
Core Data只是一个个框架,并不是数据库,它使开发者可以把数据当做对象来操作,而不必在乎数据在磁盘中的存储方式。对于iOS程序员来说,这是很有用,因此我们已经可以通过代码非常熟悉的操作对象了。
简单使用
一、对象文本上下文的创建(1和2,任选其一)
1.可以在创建Xcode时,勾选use Core Data快速创建对象文本上下文。
这样在工程,AppDelegate.h和AppDelegate.m文件中,使用CoreData需要用到的对象文本上下文,model,持久化存储调度器,以及一些方法就会被自动生成。我们只需要获取使用就是。
AppDelegate.h中
AppDelegate.m中
这样,我们只需要在使用的类里面获取所要使用的上下文就可以进行下一步了。
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;NSManagedObjectContext *context= appDelegate.managedObjectContext;
2.就是在当前类中自己手动创建
我们把context设置为属性。使用前需要导入 #import
// 创建上下文_context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
// 创建持久化存诸调动器NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
// 存诸路径NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];/*** 模型名*/NSString *sqlitePath = [path stringByAppendingPathComponent:@"Fuck.sqlite”];//
// NSLog(@"%@",sqlitePath);NSError *error = nil;
// 配置调度器的配置[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:&error];if (error) {NSLog(@"%@",error.localizedDescription);}
// 关联调试器和上下文_context.persistentStoreCoordinator= store;
二、创建实体关联模型和对象
1.创建Model
若由Xcode创建了对象文本上下文,那在工程中就已经存在以工程名为名的Data Model.
否则需要我们自己创建。进入新建文件,选择Core Data,选择Data Model,输入名字,创建即可.
配置model完成。
2、创建实体类。
进入新建文件,选择Core Data,选择NSMangerObject subclass.
选择model
选择实体
选择生成
三、进行增,删,查,改功能。
在使用的类里先导入 #import “Student.h”(以下我都写成点击事件里面)
1,增加成员对象
###pragma mark –增加
-(IBAction)addEntity:(UIButton *)sender {// 创建学生1Student *limin = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_context];limin.name = @"limin";limin.age = @(15);limin.heghit= @(1.9);// 创建学生2Student *wangwu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_context];wangwu.name = @"wangwu";wangwu.age = @(18);wangwu.heghit= @(2.1);//保存文本NSError *error = nil;[_context save:&error];if (error) {NSLog(@"%@",error.localizedDescription);}
}
2.读取成员对象
pragma mark –读取
- (IBAction)readEntity:(UIButton *)sender {// 创建查询请求NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];NSError *error =nil;// 创建查找条件// NSPredicate *pre = [NSPredicate predicateWithFormat:@"deparment.name=%@",@"iOS"];// request.predicate= pre;// 分页查找// 起始request.fetchOffset= 0;// 每页几条数据request.fetchLimit= 7;// like模糊查询也是以l开头,*为通配符(若为@"*l"则是以什么结尾。同理@"*l*");NSPredicate *pre4= [NSPredicate predicateWithFormat:@"name LIKE %@",@"l*"];request.predicate=pre4;// 排序方式NSSortDescriptor *heighSort = [NSSortDescriptor sortDescriptorWithKey:@"heigh" ascending:YES];request.sortDescriptors= @[heighSort];//执行查找NSArray *emps = [_context executeFetchRequest:request error:&error];if (error) {NSLog(@"%@",error.localizedDescription);return;}for (Student *em in emps) {NSLog(@"name=%@,age=%ld,height=%f",em.name,[em.age integerValue],[em.heghit floatValue]);}
3.更新成员对象
###pragma mark –更新
- (IBAction)updateEntity:(UIButton *)sender {// 查找到对象NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];NSPredicate *pre= [NSPredicate predicateWithFormat:@"name=%@",@"limin"];request.predicate= pre;NSError *error = nil;NSArray *emps = [_context executeFetchRequest:request error:&error];if (error) {NSLog(@"%@",error.localizedDescription);return;}// 更新内容for (Student *em in emps) {em.name = @"limingFUCK";}[_context save:nil];
}
4.删除成员对象
pragma mark –删除对象
- (IBAction)deleteEntity:(UIButton *)sender {// 查找到对象NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];NSPredicate *pre = [NSPredicate predicateWithFormat:@"name=%@",@"limin"];request.predicate = pre;NSError *error= nil;NSArray *emps= [_context executeFetchRequest:request error:&error];if (error) {NSLog(@"%@",error.localizedDescription);}// 执行删除for (Student *em in emps) {[_context deleteObject:em];}[_context save:nil];
}
工程代码分享,提取密码码:89vx。
这里写链接内容