Core Data数据持久化的使用

CoreData 是ios中用来对数据做持久化的一个框架,它对sqlite进行了封装,使我们不需要学习数据库知识,也不要写SQL语句就能将数据保存到数据库。下面来介绍CoreData的如何使用。


1. 新建一个项目,勾选使用Core Data, 新建后需要导入:CoreData.framework



2.新建项目后,AppDelegate类会生成三个属性

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

这三个对象操作数据会用的到。

3.新建一个实体对象



4. 创建一个类与实体对象关联



5. 保存一个实体模型对象

        Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
        person.personId = @10002;
        person.age = @29;
        person.name = [NSString stringWithFormat:@"jack"];
        
        NSError *error = nil;
        BOOL ret = [self.managedObjectContext save:&error];
        if (ret) {
            NSLog(@"保存成功!");
        } else {
            NSLog(@"error");
        }

6.删除一个实体模型对象

//删除
- (void)modifyPerson {
    NSEntityDescription *entifyDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *fetchReqeust = [[NSFetchRequest alloc] init];
    [fetchReqeust setEntity:entifyDesc];
    
    //查询年龄大于30的实体person对象
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.age=30"];
    [fetchReqeust setPredicate:predicate];
    //查询出来的person对象数组
    NSArray *persons = [self.managedObjectContext executeFetchRequest:fetchReqeust error:nil];
    //遍历删除
    for (Person *p in persons) {
        [self.managedObjectContext deleteObject:p];
    }
    
}

7. 查询

//根据条件查询数据
- (void)queryPerson {
    NSEntityDescription *entifyDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    //查询对象
    NSFetchRequest *fetchReqeust = [[NSFetchRequest alloc] init];
    [fetchReqeust setEntity:entifyDesc];
    
    //查询条件
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.age>40 && self.personId>10015"];
    [fetchReqeust setPredicate:predicate];
    
    //排序,按age降序排列
    NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
    [fetchReqeust setSortDescriptors:@[sortDesc]];
    
    //查询
    NSArray *persons = [self.managedObjectContext executeFetchRequest:fetchReqeust error:nil];
    for (Person *p in persons) {
        NSLog(@"name=%@,age=%@,id=%@",p.name,p.age,p.personId);
    }
}


参考资料:
Core Data Reference API listing for the Core Data classes
http://developer.apple.com/documentation/Cocoa/Reference/CoreData_ObjC/index.html

NSPredicate Reference API listing for NSPredicate
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSPredicate.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值