iOSiOS 之路iOS

iOS Core Data

2015-11-12  本文已影响772人  iOS_成才录

一、简介

二、Core Data 认识

三、Core Data 基本介绍

Core Data主要对象.png 依赖关系.png

三、注意

1、 牢记:一个数据库对应一个上下文

打开CoreData的SQL语句输出开关

四、基本使用


#import "JPViewController.h"
#import <CoreData/CoreData.h> // 导入头文件
#import "Employee.h"

@interface JPViewController (){
    NSManagedObjectContext *_context;
}

@end

@implementation JPViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //使用CoreData来保存数据
    
    //1.初始化上下文
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    
    //2.添加持久化存储
    //2.1模型文件 描述表结构的文件 也就是(Company.xcdatamodeld)这个文件
#warning 补充 ,如bundles传nil 会从主bundle加载所有的模型文件,把里面表结构都放在一个数据库文件
    NSManagedObjectModel *companyModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    
    //2.2持久化存储调用器
    NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:companyModel];
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    //数据库的完整文件路径
    NSString *sqlitePath = [doc stringByAppendingPathComponent:@"company.sqlite"];
    
    //保存一个sqite文件的话,必要知道表结构和sqlite的文件路径
    //2.3 告诉coredate数据存储在一个sqlite文件
    NSError *error = nil;
    [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:&error];
    
    if (error) {
        NSLog(@"%@",error);
    }
    
    context.persistentStoreCoordinator = store;
    _context = context;
}

- (IBAction)addEmployee {
    
    //添加员工
    //1.创建一个员工对象
    //用coredata创建对象不能使用下面的方法
    //Employee *emp = [[Employee alloc] init];
    
    
    Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
    emp.name = @"wangwu";
    emp.height = @(1.50);
    emp.createDate = [NSDate date];
    
    //保存员工信息
    NSError *error = nil;
    [_context save:&error];
    
    if (error) {
        NSLog(@"%@",error);
    }

//    for (int i = 0; i < 10; i++) {
//        Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
//        emp.name = [NSString stringWithFormat:@"zhangsan%d",i];
//        emp.height = @(1.98);
//        emp.createDate = [NSDate date];
//        
//        //保存员工信息
//        NSError *error = nil;
//        [_context save:&error];
//        
//        if (error) {
//            NSLog(@"%@",error);
//        }
//        [NSThread sleepForTimeInterval:1];
//        NSLog(@"=========%d",i);
//        
//    }
   
    
}

#pragma mark 查找员工信息
- (IBAction)findEmployee {
    
    //查找数据
    //1.创建请求对象,指定要查找的表
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
    
    //2.时间排序
    NSSortDescriptor *dateSort = [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:YES];//YES代表升序 NO 降序
    request.sortDescriptors = @[dateSort];
    
    
    //3.过滤条件 只想查找 zhangsan8
//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@",@"zhangsan8"];
//    request.predicate = pre;
    
    
    //分页语句 10 5条 (0 ,5) (5,10)
//    request.fetchOffset = 5;
//    request.fetchLimit = 5;
    
    //3.执行请求
    NSError *error = nil;
    NSArray *allEmployee =  [_context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
    
    //NSLog(@"%@",allEmployee);
    for (Employee *emp in allEmployee) {
        NSLog(@"%@ %@ %@",emp.name,emp.height,emp.createDate);
    }
}


#pragma 更新员工信息
- (IBAction)updateEmployee {
    
    //查找数据
    //1.创建请求对象,指定要查找的表
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
    
    //2.时间排序
    NSSortDescriptor *dateSort = [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:YES];//YES代表升序 NO 降序
    request.sortDescriptors = @[dateSort];
    
    
   //过滤条件 只想查找 zhangsan8
   NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@",@"zhangsan8"];
   request.predicate = pre;
    
    
    //3.执行请求
    NSArray *allEmployee =  [_context executeFetchRequest:request error:nil];
    
    NSLog(@"%@",allEmployee);
    //更新zhangsan8的身高为2米
    for (Employee *emp in allEmployee) {
        emp.height = @(2);
    }
    
    //保存更改的信息
    [_context save:nil];
}

#pragma mark 删除员工信息
- (IBAction)deleteEmployee {
    //查找数据
    //1.创建请求对象,指定要查找的表
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
    
    //2.时间排序
    NSSortDescriptor *dateSort = [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:YES];//YES代表升序 NO 降序
    request.sortDescriptors = @[dateSort];
    
    
    //过滤条件 只想查找 zhangsan8
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@",@"zhangsan8"];
    request.predicate = pre;
    
    
    //3.执行请求
    NSArray *allEmployee =  [_context executeFetchRequest:request error:nil];
    
    NSLog(@"%@",allEmployee);
    //删除zhangsan8的信息
    for (Employee *emp in allEmployee) {
        [_context deleteObject:emp];
    }

}


#pragma mark 复杂查询
- (IBAction)fuzaFind {
    //查找数据
    //1.创建请求对象,指定要查找的表
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
    
    //2.时间排序
    NSSortDescriptor *dateSort = [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:YES];//YES代表升序 NO 降序
    request.sortDescriptors = @[dateSort];
    
    
    //3.过滤条件 只想查找 zhangsan8
    //a 查找名为zhangsan 并且身高大于等1.9
    //NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@ AND height >= %lf",@"zhangsan",1.30];
    
    //b.查找名字以zh开头 并且身高大于1.3
    //以什么BEGINSWITH
    //c代表不区分大小写
    //NSPredicate *pre = [NSPredicate predicateWithFormat:@"name BEGINSWITH[c] %@ AND height >= %lf",@"abc",1.30];
    
    //c.查找名字以san结尾 ,并用身高大于1.3
    //NSPredicate *pre = [NSPredicate predicateWithFormat:@"name ENDSWITH[c] %@ AND height >= %lf",@"cent",1.30];
    
    //4.模糊查询
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name like[c] %@",@"Li*"];
    
    request.predicate = pre;
    
    //4.执行请求
    NSError *error = nil;
    NSArray *allEmployee =  [_context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
    
    //NSLog(@"%@",allEmployee);
    for (Employee *emp in allEmployee) {
        NSLog(@"%@ %@ %@",emp.name,emp.height,emp.createDate);
    }
}
@end
上一篇 下一篇

猜你喜欢

热点阅读