这里是只是代码,因为博客插入图片效果不是很好,我自己写的总结比较详细,有兴趣的朋友可以在评论里留下邮箱,我收到后会发给大家。
转载注明出处,重视原创者的劳动成果,谢谢!
- (void)viewDidLoad {
[super
viewDidLoad];
[self
_creatTable];//插入数据
// [self _query];// 查询数据
// KVC很霸道,即使readonly通过kvc也可赋值,kvo精华
// Book * book = [[Book alloc]init];
//// book.name = @"book1";
// [book setValue:@"book2" forKey:@"name"];
// NSLog(@"%@",book.name);
}
#pragma mark - _query查询
- (void)_query{
//
初始化模型
NSManagedObjectModel * model = [NSManagedObjectModel
mergedModelFromBundles:nil];
//
创建基础库协调器
NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator
alloc]initWithManagedObjectModel:model];
//
获取沙盒路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
firstObject];
path = [path stringByAppendingPathComponent:@"coreData.sqlite"];
NSLog(@"%@",path);
NSURL * url = [[NSURL
alloc]initFileURLWithPath:path];
//为协调器加载基础库路径和基础库类型
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url options:nil
error:nil];
//
创建上下文
NSManagedObjectContext * context = [[NSManagedObjectContext
alloc]init];
context.persistentStoreCoordinator = psc;
//
初始化查询请求
NSFetchRequest * request = [[NSFetchRequest
alloc]initWithEntityName:@"Teacher"];
//
谓词过滤(查询条件)
#pragma mark ‘=‘
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@",@"zhangks"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = ‘zhangks‘"];
#pragma mark ‘>‘
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age >10"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name in %@",@[@"zhangsk",@"jack"]];
#pragma mark ‘like‘模糊查询
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"zha*"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"*zha*"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"*zha"];
#pragma mark ‘and‘和‘&&‘模糊查询
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ and age = 19",@"zhangks"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ && age = 19",@"zhangks"];
#pragma mark ‘between‘模糊查询
//
方式1
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between {20,30}"];
//
方式2
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between %@",@[@"10",@"29"]];
// predict = [NSPredicate predicateWithFormat:predict.predicateFormat];
// request.predicate = predict;
//
数据排序 ascending=YES顺序,ascending=NO倒序
NSSortDescriptor * sort = [NSSortDescriptor
sortDescriptorWithKey:@"name"
ascending:YES];
request.sortDescriptors =
@[sort];
NSError * error ;
//
执行查询语句
NSArray * array = [context
executeFetchRequest:request error:&error];//
查询结果返回的是数组
// NSInteger count = [context countForFetchRequest:request error:&error];
//
查询结果的个数,返回nsinteger
// NSLog(@"%li",count);
/*
// // KVC方式获取
// for (NSManagedObject * student in array) {
// NSLog(@"————————————————————%@,%li",[student valueForKey:@"name"],[[student/Users/zhangxin/Desktop/OC/UI/5.19/5.21coreData/testtestcoredata/testtestcoredata/ViewController.m valueForKey:@"age"] integerValue]);
// }
// // 子类KVC方式获取
// for (Student * student in array) {
// NSLog(@"%li,%@",[student.age integerValue],[student valueForKey:@"name"]);
// }
*///KVC
//
通过映射对象获取
for (Teacher * teacher
in array) {
NSLog(@"%@",teacher.name);
}
}
#pragma mark _creatTable
- (void)_creatTable{
//
初始化模型
NSManagedObjectModel * model = [NSManagedObjectModel
mergedModelFromBundles:nil];
//
创建基础库协调器
NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator
alloc]initWithManagedObjectModel:model];
//
获取沙盒路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
firstObject];
path = [path stringByAppendingPathComponent:@"coreData.sqlite"];
NSLog(@"%@",path);
NSURL * url = [[NSURL
alloc]initFileURLWithPath:path];
//为协调器加载基础库路径和基础库类型
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url options:nil
error:nil];
//
创建上下文
NSManagedObjectContext * context = [[NSManagedObjectContext
alloc]init];
context.persistentStoreCoordinator = psc;
// //
插入数据
// NSManagedObject * student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];
// [student setValue:@"zhangks" forKey:@"name"];
// [student setValue:@(19) forKey:@"age"];
//
// NSManagedObject * teacher = [NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:context];
// [teacher setValue:@"zhqo" forKey:@"name"];
//
插入数据 新建 NSManagedObject subclass类 ,自动根据model中entity(实体)的表创建出类(继承自NSManagedObject,拥有NSManagedObject的属性和方法)所以直接用类名创建即可
// Student * student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];
// student.name = @"jack";
// student.age = @(29);
//
//
//
// Teacher * teacher = [NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:context];
// teacher.name = @"limei";
//
初始化查询请求
NSFetchRequest * request = [[NSFetchRequest
alloc]initWithEntityName:@"Teacher"];
//
谓词过滤(查询条件)
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = ‘limei‘"];
// request.predicate = predict;
NSArray * array = [context
executeFetchRequest:request error:nil];
for (Teacher * teacher
in array) {
Student * student1 = [NSEntityDescription
insertNewObjectForEntityForName:@"Student"
inManagedObjectContext:context];
student1.name =
@"jim";
student1.age =
@(12);
student1.relationship = teacher;
NSLog(@"%@,%@",teacher.name,student1.relationship.name);
//// teacher.name = @"liuwu";
//// [context deleteObject:teacher];
//
}
// NSSet * deleteSet = [context deletedObjects];//
删除(未save),执行删除语句后未保存前,缓存中(库表未修改)存储的删除数据
// NSSet * insertSet = [context insertedObjects];//同上
//
保存
NSError * error;
[context
save:&error];
}
ios CoreData框架的使用,对上下文数据的增删改查,表与表之间的关联,1对多,1对1,谓词查询,多表连接ios CoreData框架的使用,对上下文数据的增删改查,表与表之间的关联,1对多,1对1,谓词查询,多表连接ios CoreData框架的使用,对上下文数据的增删改查,表与表之间的关联,1对多,1对1,谓词查询,多表连接ios
CoreData框架的使用,对上下文数据的增删改查,表与表之间的关联,1对多,1对1,谓词查询,多表连接ios CoreData框架的使用,对上下文数据的增删改查,表与表之间的关联,1对多,1对1,谓词查询,多表连接ios CoreData框架的使用,对上下文数据的增删改查,表与表之间的关联,1对多,1对1,谓词查询,多表连接ios CoreData框架的使用,对上下文数据的增删改查,表与表之间的关联,1对多,1对1,谓词查询,多表连接