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. 如何解决div里面img图片下方有空白的问题?

    空白产生原因: 图片默认的vertical-align是baseline,(元素放置在父元素的基线上,也就是xx下边距) 一个inline-block元素,如果里面没有inline元素,或者overf ...

  2. php解析json字符串变量总是空白null

    通过接口获取的json字符串使用json_decode始终无法正确解析,返回空白. 直接把结果字符串复制出来手动创建一个变量却正常,在前端js也能解析,搞了半天不得其解,最后发现是接口输出的结果包含有 ...

  3. 语音按钮功能之UIButton的UIControlEventTouchUpInside没有执行问题

    新版本im功能开发过程中,遇到个奇葩的问题. 就是点击语音按钮,点击一下松开,不执行UIControlEventTouchUpInside的方法.所以导致一直在录音 但是长按2秒以上,是可以执行UIC ...

  4. MySQL创建存储过程/函数需要的权限

    alter routine---修改与删除存储过程/函数 create routine--创建存储过程/函数 execute--调用存储过程/函数 下面有一篇介绍MySQL所有权限的博文 http:/ ...

  5. 第3章 CentOS常用命令

    一.CentOS常用命令 1.1 文件和目录 # cd /home                        进入 '/home' 目录 # cd ..                       ...

  6. tomcat访问(access)日志配置、记录Post请求参数

    tomcat访问(access)日志配置.记录Post请求参数 一.配置与说明 tomcat访问日志格式配置,在config/server.xml里Host标签下加上 <Valve classN ...

  7. Xcode: This device is no longer connected error

    Quit the xcode and connect again will all right.

  8. 详解在Visual Studio中使用git版本系统(图文)

    http://www.codesky.net/article/201111/123474.html 这篇教程的预期,是希望没有任何版本使用基础的新手也可以掌握,所以细节较多,不当之处,欢迎指正. 第一 ...

  9. 3D空间中射线与轴向包围盒AABB的交叉检测算法 【转】

    http://blog.csdn.net/i_dovelemon/article/details/38342739 引言 在上一节中,我讲述了如何实现射线与三角形的交叉检测算法. 但是,我们应该知道, ...

  10. Linux中的热键[Tab] [Ctrl]-c [Ctrl]-d

    Tab键:命令或者文件补全.可以避免很多的输入错误 1. 按一次,文件或命令补全 2. 按两次,会列举出以按键前的字母为首的所有命令或者文件 Ctrl+C:中断目前程序 Ctrl+D:键盘输入结束.可 ...