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.翻转视图 使用翻转视图浏览集合中的图像或其他项目(例如相册中的照片或产品详细信息页中的项目),一次显示一个项目. 对于触摸设备,轻扫某个项将在整个集合中 ...
随机推荐
- 菜鸟学自动化测试(八)----selenium 2.0环境搭建(基于maven)
菜鸟学自动化测试(八)----selenium 2.0环境搭建(基于maven) 2012-02-04 13:11 by 虫师, 11419 阅读, 5 评论, 收藏, 编辑 之前我就讲过一种方试来搭 ...
- wexinjs 调用
public class Utils { static string appid = GetAppSettingValue("appid"); sta ...
- MySQL双机热备份
系统: CentOS release 6.6 (Final) MySQL: mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) ...
- 黑客们的故事(连载六):IT世界里的理想主义者
一头疏于梳理的长发和一把肆意生长的大胡子,往往可能是人们对于理查德马修斯托曼的第一印象.这位马上就要过60岁生日的自由软件基金会主席不用手机.不买房.没有汽车,看起来像是个流浪汉,但是却是美国工程院院 ...
- React和ES6(二)ES6的类和ES7的property initializer
React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...
- Android之Inflate()方法用途
转自:http://blog.csdn.net/andypan1314/article/details/6715928 Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的 ...
- RHEL5.8使用yum安装xclock
使用xshell连接RHEL5.8服务器,敲入xclock命令想验证图形化安装条件,但是显示无此命令. [root@template ~]# xclock -bash: xclock: command ...
- 502 Server dropped connection
在本地电脑上开启了,全局VPN代理后,出现 502 报错. 502 Server dropped connection The following error occurred while tryin ...
- Magicodes.WeiChat——多租户的设计与实现
概要 多租户(Multi Tenancy/Tenant)是一种软件架构,其定义是:在一台服务器上运行单个应用实例,它为多个租户提供服务. 本框架使用的是共享数据库.共享 Schema.共享数据表的数据 ...
- AngularJS快速入门指南09:SQL
我们可以将之前章节中的代码用来从数据库中读取数据. 通过PHP Server从MySQL数据库中获取数据 <div ng-app="myApp" ng-controller= ...