iOS进阶——coreData的使用与学习
来源:互联网 发布:苹果time capsule知乎 编辑:程序博客网 时间:2024/04/30 05:16
一、创建coreData
coreData是苹果官方推广的数据库,支持可视化。与iOS结合的更紧密。
coreData的核心对象:
coreData核心对象关系:
1.持久化存储和存储文件:
NSPersistentStore:持久化存储,是对实际文件的一种object-C的表示方式。
存储文件:iOS支持四种存储类型。
NSSQLiteStoreType,NSBinaryStoretype,NSInMemoryStoreType,NSXMLStoreType。
2.被管理对象上下文:
NSManagedObjectContext:负责应用和数据库之间的交互。
数据的保存要用NSManagedObjectContext进行Save操作。
同理,查询等也需要他来进行。
3.被管理对象的相关类
NSmanagedObjectMode:被管理对象模型。(在操作中一直是以可视化的方式操作的)。
NSManagedObject:实体描述类,一般讲数据库中的表转化为模型时,都是这个类的子类。
Entity:实体类,实体是对文件数据的描述(这个解释比较抽象,通俗理解为在每次需要交互的时候,都由这个类来反馈数据库中的某个表(个人理解))。
下面附上一段插入数据和查询数据的代码,用来理解coreData。
-(void)insertData{// NSEntityDescription *entity = [NSEntityDescription entityForName:@"LanOuclass" inManagedObjectContext:[self context]];// LanOuClass *lanOuClass = [[LanOuClass alloc]initWithEntity:entity insertIntoManagedObjectContext:[self context]]; //创建被管理实例对象Entity LanOuClass *lanOuClass = [NSEntityDescription insertNewObjectForEntityForName:@"LanOuClass" inManagedObjectContext:[self context]]; //给实例对象赋值 lanOuClass.class_Id = @(6660); lanOuClass.stu_count = @(10); //提前显示 [self.dataMutableArr addObject:lanOuClass]; NSIndexPath* indexPath =[NSIndexPath indexPathForRow:self.dataMutableArr.count - 1 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; //滚动到最后一行 [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; //给实例化对象里的关联属性赋值;是个NSSet属性,有多个Student对象 NSArray *nameArray = [NSArray arrayWithObjects:@"王",@"李",@"赵",@"钱",@"孙",@"周",@"吴",@"郑",@"冯",@"陈",nil ]; NSArray *genderArray = [NSArray arrayWithObjects:@"男",@"女",nil]; for(int i = 0; i<nameArray.count; i++ ){ Student* stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:[self context]]; stu.name = nameArray[i]; stu.age = @(20+i); stu.gender =genderArray[arc4random()%2]; //将学生和班级建立关联 [lanOuClass addStudentObject:stu]; } //保存对象,是由context对象执行,在appdelegate里面有详细的执行方法 [[self appDelegate] saveContext]; }
再附上一段查询的代码:
//查询方法-(void)fetchData{ NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"LanOuClass" inManagedObjectContext:[self context]]; [fetchRequest setEntity:entity]; // 添加查询条件语句// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<#format string#>", <#arguments#>];// [fetchRequest setPredicate:predicate]; // 按某个字段对结果排序 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"class_Id" ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; NSError *error = nil; NSArray *fetchedObjects = [[self context] executeFetchRequest:fetchRequest error:&error]; if (fetchedObjects == nil) { NSLog(@"空"); } self.dataMutableArr = [fetchedObjects mutableCopy];}
二、数据库版本迁移
在appdelegate.m里,做如下设置:
options原本为空,自行添加代码,使其支持版本迁移。
然后选中xcode,点击数据库,在editor选项卡中,选择add model version。
这样,就可以创建新的数据库,并修改里面的值了 。
#pragma mark - coreData-(AppDelegate *)appdelegate{ return [UIApplication sharedApplication].delegate;}-(NSManagedObjectContext*)context{ return [[self appdelegate] managedObjectContext];}
coreData操作是由appdelegate完成的,上面是得到当前appdelegate对象的方法。
0 0
- iOS进阶——coreData的使用与学习
- CoreData—使用进阶
- coredata 进阶篇——IOS之分析网易新闻存储数据(CoreData的使用,增删改查)
- 认识CoreData—使用进阶
- 认识CoreData—使用进阶
- IOS - CoreData的使用
- iOS CoreData的使用
- IOS对象关系映射(ORM)之coreData框架的学习——(一)基本使用
- XMPP框架 微信项目开发之CoreData学习——CoreData的基本使用
- ios中的coredata的使用
- IOS CoreData的初始化使用
- ios中的coredata的使用
- ios中的coredata的使用
- ios中的coredata的使用
- iOS CoreData的基本使用
- ios中coredata的使用
- ios中的coredata的使用
- ios中的coredata的使用
- Conexant HD audio无法内录的问题
- 2016春季学习——追逐
- Problem C: C语言习题 字符串长度
- KNN及其改进算法的python实现
- 树状数组与逆序对数
- iOS进阶——coreData的使用与学习
- CodeForces 632A(反向模拟)
- MySQL知识点整理
- Problem D: C语言习题 字符串比较
- NVIDIA Jetson TK1学习与开发(二):入门指导
- 自定义view 之 网格
- 第二周项目三小试循环
- php的学习笔记之面向对象(六)static
- AlphaGo来了,终结者不远了