CoreData基础教程

使用CoreData来保存数据

1.创建DataModel,创建Entity,添加属性,创建NSManagedObject subclass关联创建的实体类。自动生成代码类,然后在对应的viewController导入实体类头文件。

//  1. 初始化上下文
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
// 2. 添加持久化存储
// 2.1 模型文件 描述表结构的文件
NSManagedObjectModel *companyModel = [NSManagedObjectModel mergedModelFromBundles:nil];

// 2.2 持久化存储调用器
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:companyModel];
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

// 数据库的完整路径
NSString *sqlitePath = [doc stringByAppendingString:@"company.sqlite"];
// 保存一个sqlite文件的话,必须要知道表结构和sqlite的文件路径

// 2.3 告诉coredata数据存储在一个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)add:(id)sender {
// 添加员工
// 1.创建一个员工对象
// 用coredata创建对象不能使用下面的方法
    //    Employee *emp = [[Employee alloc] init];
Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
emp.name = @"liqinghai";
emp.height = @(2.0);
emp.createDate = [NSDate date];

// 保存员工信息
NSError *error = nil;
[_context save:&error];
if (error) {
    NSLog(@"%@",error);
}
    }
- (IBAction)dele:(id)sender {
//查找数据
//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 = %@",@"liqinghai"];
request.predicate = pre;


//3.执行请求
NSArray *allEmployee =  [_context executeFetchRequest:request error:nil];

// 4. 删除信息
for (Employee *emp in allEmployee) {
    [_context deleteObject:emp];
}
}
  - (IBAction)update:(id)sender {
// 查找数据
// 1. 创建请求对象, 指定要查找的表
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];

// 2. 时间排序
NSSortDescriptor *dateSort = [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:YES];
request.sortDescriptors = @[dateSort];

// 3. 过滤条件 只想查找 zhangsan8
NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@", @"liqinghai"];
request.predicate = pre;

// 3. 执行请求
NSError *error = nil;
NSArray *allEmployee = [_context executeFetchRequest:request error:&error];

// 4. 更新
for (Employee *emp in allEmployee) {
    emp.height = @(12);
    emp.name = @"zhangbo";
}
[_context save:&error];
}

- (IBAction)fetch:(id)sender {
// 查询数据
// 1. 创建请求对象,指定要查找的表
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];

// 2. 时间排序
NSSortDescriptor *dateSort = [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:YES];
request.sortDescriptors = @[dateSort];

// 3. 过滤条件 只想查找 zhangsan8
NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@", @"liqinghai"];
request.predicate = pre;

// 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);
  } 
}

你可能感兴趣的:(CoreData基础教程)