ios深度解析之coreData

coreData数据持久化 是对SQLite的升级优化, 这里有几个非常重要的类:

1.NSManagerObjectModel(被管理对象的模型, 实体) 
2.NSEntityDescription (创建实体描述, 实体名必须跟类名相同)
3,NSFetchRequest (从实体中查询)
4.NSManagedObjectContext(被管理对象上下文, 临时数据库(添加, 查询,修改,删除数据))
5.NSPrisistenStoreCoordinator(持久化存储助理 , 数据库的连接器)
6.创建表步骤:

(1) ctrl +  n新建

(2)选中所建项目

(3)选中项目 添加数据

(4)在工程里自动会生成.h和.m文件


7.环境搭建上下文

#pragma mark - 添加数据
- (IBAction)tianjia:(UIButton *)sender {
    
    // 创建实体描述
    NSEntityDescription *studentED = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerContext];

    for (int i = 0; i < 10; i++) {
        // 根据实体描述创建对象
        Student *student = [[Student alloc] initWithEntity:studentED insertIntoManagedObjectContext:self.managerContext];
        
        // 给实体赋值
        student.name = [NSString stringWithFormat:@"毛毛%d号", i];
        student.age = [NSNumber numberWithInt: 18 + i*5];
        student.sex = [NSString stringWithFormat:@"女"];
    }
    
    // 数据同步
    NSError *error = nil;
    [self.managerContext save:&error];
    if (!error) {
        
        NSLog(@"存储成功");
    }
    
}

#pragma mark - 查询数据
- (IBAction)chaXun:(UIButton *)sender {
    
    // 从实体中查询
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 创建实体描述
    NSEntityDescription *studetED = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerContext];
    
    // 设置查询实体
    request.entity = studetED;
    
    // 设置排序 yes升序 no降序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    request.sortDescriptors = @[sort];
    
    
    //根据条件查询
//  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", @"毛毛7号"];
    
//  模糊查询 包含
//  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS%@", @"7"];
    
//  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE %@", @"毛毛*号"];
    
//  request.predicate = predicate;
    
   
    // 数组接收查询数据
    NSArray *array = [self.managerContext executeFetchRequest:request error:nil];
    
    // 遍历数组 取出数据打印
    for (Student *student in array) {
        
        NSLog(@"姓名:%@, 年纪:%@, 性别:%@", student.name, student.age, student.sex);
    }
    
    
}

#pragma mark - 修改数据
- (IBAction)xiuGai:(UIButton *)sender {
    
    // 从实体中修改
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 创建实体描述
    NSEntityDescription *StudentED = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerContext];
    
    
    // 设置修改的实体
    request.entity = StudentED;
    
    // 根据条件修改
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", @"毛毛1号"];
    request.predicate = predicate;
    
    // 数据接收修改数据
    NSArray *array = [self.managerContext executeFetchRequest:request error:nil];
    
    if (array.count > 0) {
        Student *student = array.lastObject;
        student.name = @"军军";
        student.age = @1;
        student.sex = @"男";
        
        // 同步数据
        if ([self.managerContext save:nil]) {
            
            NSLog(@"修改成功");
        } else {
        
            NSLog(@"修改失败");
        }
    } else {
        NSLog(@"没有对应的实体");
    }
    
}

#pragma mark - 删除数据
- (IBAction)shanChu:(UIButton *)sender {
    
    
    // 从实体中删除
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 创建实体描述
    NSEntityDescription *StudentED = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerContext];
    
    // 设置删除的实体
    request.entity = StudentED;
    
    // 根据条件删除
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sex = %@", @"女"];
    
    request.predicate = predicate;
    
    // 数据接收删除数据
    NSArray *array = [self.managerContext executeFetchRequest:request error:nil];
    
    if (array.count > 0) {
        // 遍历数组
        for (Student *stu in array) {
        // 删除数据
        [self.managerContext deleteObject:stu];
         
        // 同步数据
        if ([self.managerContext save:nil]) {
            
            NSLog(@"删除成功");
        } else {
        
            NSLog(@"删除失败");
        }
    }
    
    }else  {
        NSLog(@"没事这个实体");
    }
    
    
    
}

#pragma mark - 被管理对象上下的懒加载方法
- (NSManagedObjectContext *)managerContext
{
    if (! _managerContext) {
        
        // 获取apdelegate当中的被管理对象上下文
        _managerContext  = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
        
    }
    
    return _managerContext;
    
}














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值