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.翻转视图 使用翻转视图浏览集合中的图像或其他项目(例如相册中的照片或产品详细信息页中的项目),一次显示一个项目. 对于触摸设备,轻扫某个项将在整个集合中 ...
随机推荐
- zt-Simple source policy routing
原文地址: http://tldp.org/HOWTO/Adv-Routing-HOWTO/lartc.rpdb.simple.html Linux Advanced Routing & Tr ...
- DBHelper
DBHelper: using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- cloudera manager安装步骤小结
1.准备三台虚拟机,系统是centos 7,IP分别是: 192.168.254.110 master 192.168.254.111 slave1 192.168.254.112 slave2 2. ...
- 【原创】-- nfs安装配置及使用,挂载根文件系统
环境:ubuntu14.04(非虚拟机),OK6410 环境搭建: (1) #sudo apt-get install nfs-kernel-server 如果已经是最新版本了,无需安装 (2) 建立 ...
- IOS笔记之UIKit_UIAlertView、UIActionSheet
//首先必须继承协议 @interface TRViewController : UIViewController<UIAlertViewDelegate,UIActionSheetDelega ...
- WP8:在Unity中使用OpenXLive
Unity 4.2正式版开始添加了对Windows 8.Windows Phone 8等其他平台的支持,而且开发者可以免费使用Unity引擎来开发游戏了.而作为Windows Phone和Window ...
- 用c#开发微信 (9) 微渠道 - 推广渠道管理系统 4 部署测试 (最终效果图)
我们可以使用微信的“生成带参数二维码接口”和 “用户管理接口”,来实现生成能标识不同推广渠道的二维码,记录分配给不同推广渠道二维码被扫描的信息.这样就可以统计和分析不同推广渠道的推广效果. 本文是微渠 ...
- 解决阿里云SLB无法添加https证书的问题
私钥是在Linux中通过下面的openssl命令生成的: openssl req -new -newkey rsa:2048 -nodes -keyout cnblogs.key -out cnblo ...
- 曲演杂坛--EXISTS语句
通常在我写EXISTS语句时,我会写成IF EXISTS(SELECT TOP(1) 1 FROM XXX),也没细细考究过为什么要这么写,只是隐约认为这样写没有啥问题,那今天就深究下吧! 首先准备测 ...
- [chrome插件分享] gitlab-tree 更方便的浏览Gitlab上的代码
说明 经常玩Github的人肯定都知道大名鼎鼎的octotree吧,这款chrome插件可以说是浏览代码的神器,利用左侧的树形菜单可以很方便的打开目录.浏览文件等,加上Github全站本身使用了pja ...