1.如果想创建一个带有coreData的程序,要在项目初始化的时候勾选中

2.创建完成之后,会发现在AppDelegate里多出了几个属性,和2个方法

  1. <span style="font-size:18px;">
  2. @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
  3. @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
  4. @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  5. - (void)saveContext;
  6. - (NSURL *)applicationDocumentsDirectory;</span>

managedObjectContext (被管理的数据上下文)操作实际内容(操作持久层)作用:插入数据,查询数据,删除数据

NSManagedObjectModel(被管理的数据模型)数据库所有表格或数据结构,包含各实体的定义信息 作用:添加实体的属性,建立属性之间的关系操作方法:视图编辑器,或代码

NSPersistentStoreCoordinator(持久化存储助理)相当于数据库的连接器 作用:设置数据存储的名字,位置,存储方式,和存储时机

方法saveContext表示:保存数据到持久层(数据库)

方法applicationDocumentsDirectory表示:应用程序沙箱下的Documents目录路径

3.如果想创建一个实体对象的话,需要点击.xcdatamodel,Add Entity,添加想要的字段

4.生成对象文件,command+n,然后选中CoreData里的NSManagerObjectSubClass进行关联,选中实体创建

5.添加数据

  1. Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
  2. if (newPerson == nil){
  3. NSLog(@"Failed to create the new person.");
  4. return NO;
  5. }
  6. newPerson.firstName = paramFirstName;
  7. newPerson.lastName = paramLastName;
  8. newPerson.age = [NSNumber numberWithUnsignedInteger:paramAge];
  9. NSError *savingError = nil;
  10. if ([self.managedObjectContext save:&savingError]){
  11. return YES;
  12. } else {
  13. NSLog(@"Failed to save the new person. Error = %@", savingError);
  14. }

NSEntityDescription(实体结构)相当于表格结构

6.取出数据查询

  1. /* Create the fetch request first */
  2. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  3. /* Here is the entity whose contents we want to read */
  4. NSEntityDescription *entity =
  5. [NSEntityDescription
  6. entityForName:@"Person"
  7. inManagedObjectContext:self.managedObjectContext];
  8. /* Tell the request that we want to read the
  9. contents of the Person entity */
  10. [fetchRequest setEntity:entity];
  11. NSError *requestError = nil;
  12. /* And execute the fetch request on the context */
  13. NSArray *persons =
  14. [self.managedObjectContext executeFetchRequest:fetchRequest
  15. error:&requestError];
  16. /* Make sure we get the array */
  17. if ([persons count] > 0){
  18. /* Go through the persons array one by one */
  19. NSUInteger counter = 1;
  20. for (Person *thisPerson in persons){
  21. NSLog(@"Person %lu First Name = %@",
  22. (unsigned long)counter,
  23. thisPerson.firstName);
  24. NSLog(@"Person %lu Last Name = %@",
  25. (unsigned long)counter,
  26. thisPerson.lastName);
  27. NSLog(@"Person %lu Age = %ld",
  28. (unsigned long)counter,
  29. (unsigned long)[thisPerson.age unsignedIntegerValue]);
  30. counter++;
  31. }
  32. } else {
  33. NSLog(@"Could not find any Person entities in the context.");
  34. }

7.删除数据

  1. /* Create the fetch request first */
  2. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  3. /* Here is the entity whose contents we want to read */
  4. NSEntityDescription *entity =
  5. [NSEntityDescription
  6. entityForName:@"Person"
  7. inManagedObjectContext:self.managedObjectContext];
  8. /* Tell the request that we want to read the
  9. contents of the Person entity */
  10. [fetchRequest setEntity:entity];
  11. NSError *requestError = nil;
  12. /* And execute the fetch request on the context */
  13. NSArray *persons =
  14. [self.managedObjectContext executeFetchRequest:fetchRequest
  15. error:&requestError];
  16. if ([persons count] > 0){
  17. /* Delete the last person in the array */
  18. Person *lastPerson = [persons lastObject];
  19. [self.managedObjectContext deleteObject:lastPerson];
  20. NSError *savingError = nil;
  21. if ([self.managedObjectContext save:&savingError]){
  22. NSLog(@"Successfully deleted the last person in the array.");
  23. } else {
  24. NSLog(@"Failed to delete the last person in the array.");
  25. }
  26. } else {
  27. NSLog(@"Could not find any Person entities in the context.");
  28. }

8.排序

    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;

    //  注意 ascending:YES 属性决定排序顺序

CoreData使用的更多相关文章

  1. iOS基本数据库存储方式 - CoreData

    CoreData 创建模型文件的过程 1.选择模板 2.添加实体 3.添加实体的属性[注意]属性的首字母必须小写 一.CoreData管理类(必备以下三个类对象) 1.CoreData数据操作的上下文 ...

  2. iOS CoreData 中 objectID 的不变性

    关于 CoreData的 objectID 官方文档有这样的表述:新建的Object还没保存到持久化存储上,那么它的objectID是临时id,而保存之后,就是持久化的id,不会再变化了. 那么,我想 ...

  3. CoreData __ 基本原理

    操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context          Co ...

  4. iOS CoreData primitive accessor

    Given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstNa ...

  5. 初识CoreData与详解

    Core Data数据持久化是对SQLite的一个升级,它是iOS集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类. (1)NSManagedObjectModel(被管 ...

  6. CoreData教程

    网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...

  7. CoreData和SQLite多线程访问时的线程安全

    关于CoreData和SQLite多线程访问时的线程安全问题 数据库读取操作一般都是多线程访问的.在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱.IOS中常 ...

  8. IOS数据存储之CoreData使用优缺点

    前言: 学习了Sqlite数据之后认真思考了一下,对于已经习惯使用orm数据库的开发者或者对sql语句小白的开发者来说该如何做好数据库开发呢?这个上网搜了一下?看来总李多虑了!apple 提供了一种数 ...

  9. iOS开发之表视图爱上CoreData

    在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功 ...

  10. CoreData

    之前在学习使用SQLite时, 需要编写大量的sql语句,完成数据的增删改查,但对于不熟悉sql语句的开发人员来说,难度较大,调试程序比较困难. 由此出现CoreData框架,将sql的操作转换成为对 ...

随机推荐

  1. AC日记——城市 洛谷 P1401

    题目描述 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最长的边的长度最小,边不能 ...

  2. 总结下web开发中基础性的常识

    一,HTML/5 1,浏览器渲染过程 主流浏览器渲染过程叫法有区别,但是主要流程还是相同的.Gecko 将视觉格式化元素组成的树称为“框架树”.每个元素都是一个框架.WebKit 使用的术语是“呈现树 ...

  3. HDU 5972 Regular Number(字符串shift - and算法)

    题目链接  HDU5972 2016 ACM/ICPC 大连区域赛 B题 我们预处理出$b[i][j]$,$b[i][j] = 1$的意义是数字$i$可以放在第$j$位. 然后就开始这个匹配的过程. ...

  4. Java中String/StringBuffer/StringBuilder区别(转)

    1.三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuild ...

  5. BT服务器的搭建(tracker-P2P服务器架设)(转)

    文章虽然有点老,但原理差不多. 继上一篇文章(http://www.cnblogs.com/EasonJim/p/6601146.html)介绍了BT的原理,现在来看下BT服务端搭建的原理. 一.BT ...

  6. 通过HTML5 FileReader实现上传图片预览功能

    原文:http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201706224590.html 在上传图片到服务器之前,我们希望可以预览一下要上传的图片. ...

  7. jmeter的master、slave模型启动方法

    机器A为master:机器B为slave:可以一个master挂多个slave,方法就是-R参数后面跟一个逗号分割的IP列表 slave启动命令:./jmeter-server -Djava.rmi. ...

  8. iOS UI08_TableView界面传值

    实现两个界面之间内容的传递 // // MainViewController.m // UI08_TableView界面传值 // // Created by dllo on 15/8/7. // C ...

  9. 百科知识 DCR文件如何打开

    使用IE可以打开,但是需要先安装Adobe Shockwave Player 天空软件下载地址:http://fpdownload.macromedia.com/get/shockwave/defau ...

  10. 记事本中写c/c++程序在Windows下运行

     1.在桌面新建一个1.c,内容例如以下: 2.打开cmd命令行窗体,进入CodeBlock安装文件夹下的:E:\Installed\CodeBlocks\MinGW.然后运行mingwvars. ...