oreData的学习记录
1.如果想创建一个带有coreData的程序,要在项目初始化的时候勾选中
2.创建完成之后,会发现在AppDelegate里多出了几个属性,和2个方法
- <span style="font-size:18px;">
- @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
- @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- - (void)saveContext;
- - (NSURL *)applicationDocumentsDirectory;</span>
managedObjectContext (被管理的数据上下文)操作实际内容(操作持久层)作用:插入数据,查询数据,删除数据
NSManagedObjectModel(被管理的数据模型)数据库所有表格或数据结构,包含各实体的定义信息 作用:添加实体的属性,建立属性之间的关系操作方法:视图编辑器,或代码
NSPersistentStoreCoordinator(持久化存储助理)相当于数据库的连接器 作用:设置数据存储的名字,位置,存储方式,和存储时机
方法saveContext表示:保存数据到持久层(数据库)
方法applicationDocumentsDirectory表示:应用程序沙箱下的Documents目录路径
3.如果想创建一个实体对象的话,需要点击.xcdatamodel,Add Entity,添加想要的字段
4.生成对象文件,command+n,然后选中CoreData里的NSManagerObjectSubClass进行关联,选中实体创建
5.添加数据
- Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
- if (newPerson == nil){
- NSLog(@"Failed to create the new person.");
- return NO;
- }
- newPerson.firstName = paramFirstName;
- newPerson.lastName = paramLastName;
- newPerson.age = [NSNumber numberWithUnsignedInteger:paramAge];
- NSError *savingError = nil;
- if ([self.managedObjectContext save:&savingError]){
- return YES;
- } else {
- NSLog(@"Failed to save the new person. Error = %@", savingError);
- }
NSEntityDescription(实体结构)相当于表格结构
6.取出数据查询
- /* Create the fetch request first */
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
- /* Here is the entity whose contents we want to read */
- NSEntityDescription *entity =
- [NSEntityDescription
- entityForName:@"Person"
- inManagedObjectContext:self.managedObjectContext];
- /* Tell the request that we want to read the
- contents of the Person entity */
- [fetchRequest setEntity:entity];
- NSError *requestError = nil;
- /* And execute the fetch request on the context */
- NSArray *persons =
- [self.managedObjectContext executeFetchRequest:fetchRequest
- error:&requestError];
- /* Make sure we get the array */
- if ([persons count] > 0){
- /* Go through the persons array one by one */
- NSUInteger counter = 1;
- for (Person *thisPerson in persons){
- NSLog(@"Person %lu First Name = %@",
- (unsigned long)counter,
- thisPerson.firstName);
- NSLog(@"Person %lu Last Name = %@",
- (unsigned long)counter,
- thisPerson.lastName);
- NSLog(@"Person %lu Age = %ld",
- (unsigned long)counter,
- (unsigned long)[thisPerson.age unsignedIntegerValue]);
- counter++;
- }
- } else {
- NSLog(@"Could not find any Person entities in the context.");
- }
7.删除数据
- /* Create the fetch request first */
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
- /* Here is the entity whose contents we want to read */
- NSEntityDescription *entity =
- [NSEntityDescription
- entityForName:@"Person"
- inManagedObjectContext:self.managedObjectContext];
- /* Tell the request that we want to read the
- contents of the Person entity */
- [fetchRequest setEntity:entity];
- NSError *requestError = nil;
- /* And execute the fetch request on the context */
- NSArray *persons =
- [self.managedObjectContext executeFetchRequest:fetchRequest
- error:&requestError];
- if ([persons count] > 0){
- /* Delete the last person in the array */
- Person *lastPerson = [persons lastObject];
- [self.managedObjectContext deleteObject:lastPerson];
- NSError *savingError = nil;
- if ([self.managedObjectContext save:&savingError]){
- NSLog(@"Successfully deleted the last person in the array.");
- } else {
- NSLog(@"Failed to delete the last person in the array.");
- }
- } else {
- NSLog(@"Could not find any Person entities in the context.");
- }
8.排序
- <pre code_snippet_id="243955" snippet_file_name="blog_20140319_5_4289257" name="code" class="objc">NSSortDescriptor *ageSort =
- [[NSSortDescriptor alloc] initWithKey:@"age"
- ascending:YES];
- NSSortDescriptor *firstNameSort =
- [[NSSortDescriptor alloc] initWithKey:@"firstName"
- ascending:YES];
- NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:
- ageSort,
- firstNameSort, nil nil];
- fetchRequest.sortDescriptors = sortDescriptors; </pre><p></p>
- <pre></pre>
- <p></p>
- <p style="background-color:rgb(255,255,255); margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,'ms song',Arial,Helvetica,sans-serif; line-height:19.09090805053711px">
- <span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br>
- </span></span></p>
- <span style="background-color:rgb(255,255,255)">注意</span><span style="font-size:18px; background-color:rgb(255,255,255)">ascending:YES 属性决定排序顺序</span><span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br>
- <br>
- <br>
- </span></span><br>
oreData的学习记录的更多相关文章
- Quartz 学习记录1
原因 公司有一些批量定时任务可能需要在夜间执行,用的是quartz和spring batch两个框架.quartz是个定时任务框架,spring batch是个批处理框架. 虽然我自己的小玩意儿平时不 ...
- Java 静态内部类与非静态内部类 学习记录.
目的 为什么会有这篇文章呢,是因为我在学习各种框架的时候发现很多框架都用到了这些内部类的小技巧,虽然我平时写代码的时候基本不用,但是看别人代码的话至少要了解基本知识吧,另外到底内部类应该应用在哪些场合 ...
- Apache Shiro 学习记录4
今天看了教程的第三章...是关于授权的......和以前一样.....自己也研究了下....我觉得看那篇教程怎么说呢.....总体上是为数不多的精品教程了吧....但是有些地方确实是讲的太少了.... ...
- UWP学习记录12-应用到应用的通信
UWP学习记录12-应用到应用的通信 1.应用间通信 “共享”合约是用户可以在应用之间快速交换数据的一种方式. 例如,用户可能希望使用社交网络应用与其好友共享网页,或者将链接保存在笔记应用中以供日后参 ...
- UWP学习记录11-设计和UI
UWP学习记录11-设计和UI 1.输入和设备 通用 Windows 平台 (UWP) 中的用户交互组合了输入和输出源(例如鼠标.键盘.笔.触摸.触摸板.语音.Cortana.控制器.手势.注视等)以 ...
- UWP学习记录10-设计和UI之控件和模式7
UWP学习记录10-设计和UI之控件和模式7 1.导航控件 Hub,中心控件,利用它你可以将应用内容整理到不同但又相关的区域或类别中. 中心的各个区域可按首选顺序遍历,并且可用作更具体体验的起始点. ...
- UWP学习记录9-设计和UI之控件和模式6
UWP学习记录9-设计和UI之控件和模式6 1.图形和墨迹 InkCanvas是接收和显示墨迹笔划的控件,是新增的比较复杂的控件,这里先不深入. 而形状(Shape)则是可以显示的各种保留模式图形对象 ...
- UWP学习记录8-设计和UI之控件和模式5
UWP学习记录8-设计和UI之控件和模式5 1.日历.日期和时间控件 日期和时间控件提供了标准的本地化方法,可供用户在应用中查看并设置日期和时间值. 有四个日期和时间控件可供选择,选择的依据如下: 日 ...
- UWP学习记录7-设计和UI之控件和模式4
UWP学习记录7-设计和UI之控件和模式4 1.翻转视图 使用翻转视图浏览集合中的图像或其他项目(例如相册中的照片或产品详细信息页中的项目),一次显示一个项目. 对于触摸设备,轻扫某个项将在整个集合中 ...
随机推荐
- EXCEL某列长度超过255个字符导入SQL SERVER的处理方法
问题描述: [Excel 源 [1]] 错误: 输出“Excel 源输出”(9) 上的 输出列“Description 3”(546) 出错.返回的列状态是:“文本被截断,或者一个或多个字符在目标代码 ...
- PXE-kickstart无人值守批量装机
服务器的批量部署: 规模化:同时装配多台服务器 自动化:安装系统.配置各种服务 远程实现:不需要光盘.U盘等安装介质 PXE,Pre-boot eXcution Environment 预启动执行环境 ...
- HTML回顾
<frameset>和<body>是同一级的,已经在html5中被弃用 结合----> 效果 注意::::span标签不自动换行****
- spring常见异常
1.ClassNotFoundException: org.springframework.dao.support.DaoSupport(解决:导入spring-tx) 2.NoClassDefFou ...
- Linux Kernel 3.11 正式版发布
Linus 发布 了 3.11 版本的 Linux 内核.该版本值得关注的新特性有: Lustre 分布式文件系统.透明的 ARM 架构的大数据页支持:ARM64 上的 Xen 和 KVM 虚拟化:O ...
- 给“.Net工资低”争论一个了结吧!
昨天我写了一篇<工资低的.Net程序员,活该你工资低>,底下的支持.争吵.骂娘的评论依旧像之前几篇园友的博客一样繁荣.公说公有理,婆说婆有理,这样争吵下去永远没有尽头.数据没有情绪,是公正 ...
- UI创意求助:手机贪吃蛇游戏方向控制键设计
继上一片博文<做梦想起来的C#简单实现贪吃蛇程序(LinQ + Entity)>之后,尝试做一个手机版本,大部分的功能都已经实现了.但在贪吃蛇的方向控制设计上一直不太满意.由于手机界面的大 ...
- git log控制输出宽度
%<(N, trunc) 下一个单元的输出宽度限制为N列, 左对齐 %<|(N, trunc) 下一个单元输出至全局第N列, 左对齐 %>, %>|, %>>, % ...
- 自制操作系统(二) 让bootsector开机启动打印一首诗
qq:992591601 欢迎交流 2016-03-31作 2016-06-01.2016-06-27改 我总结了些基本原理: 1.软盘的第一个扇区为启动区 2.计算机读软盘是以512字节为单位来读写 ...
- Spring MVC + jpa框架搭建,及全面分析
一,hibernate与jpa的关系 首先明确一点jpa是什么?以前我就搞不清楚jpa和hibernate的关系. 1,JPA(Java Persistence API)是Sun官方提出的Java持久 ...