CoreData用于做数据持久化,适合大数据量的存储和查询
CoreData不是数据库 CoreData可以使用数据库 ,XML等方式来存储数据
CoreData使用面向对象的方式操作数据
CoreData操作数据无需编写SQL语句
使用时 需要导入CoreData框架
//----------------------------------------------------------
NSManagedObjectContext 负责应用和数据库之间的交互
NSPersistentStoreCoordinator 添加持久化存储库(初始化后 给其初始化一个数据文件)
NSManagedObjectModel 被管理的对象模型
NSEntityDescription :实体描述 向实体中添加数据-->给context保管
#import <CoreData/CoreData.h>
#import "User.h"
#import "Movie.h"
@interface ViewController (){
NSManagedObjectContext *_context;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//打开数据库
[self openDataBase];
//添加数据
// [self addUser];
//查询数据
[self queryData];
//修改数据
// [self updateData];
//删除数据
[self deleteData];
NSLog(@"-------------------------");
[self queryData];
}
- (void)openDataBase{
//1.NSManagedObjectModel 加载数据模型文件
NSURL *url = [[NSBundle mainBundle]URLForResource:@"MyData" withExtension:@"momd"];
NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:url];
//2.创建 持久化存储库 带有(载入)数据模型文件
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:objectModel];
//持久化存储库 存放到路径
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/MyData.sqlite"];
NSLog(@"filePath : %@",filePath);
NSURL *url1 = [NSURL fileURLWithPath:filePath];
NSError *error = nil;
//3.设置 持久化存储库
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url1 options:nil error:&error];
if (error) {
NSLog(@"开启失败");
}else{
NSLog(@"开启成功");
}
_context = [[NSManagedObjectContext alloc]init];
//context 中的数据存储到指定的 "psc" 中(一次指定后面都不会用psc了) -->后面所有操作对context操作
_context.persistentStoreCoordinator = psc;
}
- (void)addUser{
//添加 实体 数据---->向User实体中添加数据-->给context保管
User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_context];
user.name = @"张杰";
user.age = @22;
user.height = @177;
Movie *movie = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:_context];
movie.movieName = @"速度与激情";
movie.movieID = @1003;
movie.price = @152;
Movie *movie1 = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:_context];
movie1.movieName = @"恋爱男女";
movie1.movieID = @1004;
movie1.price = @112;
//添加数据后保存数据
BOOL isSuccess = [_context save:nil];
if (isSuccess) {
NSLog(@"添加成功");
}else{
NSLog(@"添加失败");
}
}
- (void)queryData{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];
NSFetchRequest *request2 = [NSFetchRequest fetchRequestWithEntityName:@"Movie"];
// 在context中查询(context相当于容器)
NSArray *dataArr = [_context executeFetchRequest:request error:nil];
NSArray *arr2 = [_context executeFetchRequest:request2 error:nil];
for (User *model in dataArr) {
NSLog(@"%@ %@ %@",model.name,model.age,model.height);
}
for (Movie *model in arr2) {
NSLog(@"%@ %@ %@",model.movieName,model.movieID,model.price);
}
}
//修改数据
- (void)updateData{
//查询找到需要修改的数据
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Movie"];
//请求 设置请求条件
request.predicate = [NSPredicate predicateWithFormat:@"movieID = 1004"];
//查询-->结果
NSArray *dataArr = [_context executeFetchRequest:request error:nil];
//拿到查询到的数据
for (Movie *model in dataArr) {
//遍历出来 修改数据
model.movieName = @"大话西游";
}
//修改完成 保存
BOOL isSuccess = [_context save:nil];
if (isSuccess) {
NSLog(@"修改成功");
}else{
NSLog(@"修改失败");
}
}
- (void)deleteData{
//查询数据
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Movie"];
request.predicate = [NSPredicate predicateWithFormat:@"movieID=1004"];
NSArray *arr =[_context executeFetchRequest:request error:nil];
for (Movie *model in arr) {
//context删除数据
[_context deleteObject:model];
}
//删除数据后保存
[_context save:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.