1. #import "AppDelegate.h"
  2. #import "Person.h"
  3. @implementation AppDelegate
  4.  
  5. @synthesize managedObjectContext = _managedObjectContext;
  6. @synthesize managedObjectModel = _managedObjectModel;
  7. @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
  8.  
  9. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  10. {
  11. [self createNewPersonWithFirstName:@"Anthony" lastName:@"Robbins" age:51];
  12. [self createNewPersonWithFirstName:@"Richard" lastName:@"Branson" age:61];
  13.  
  14. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Person"];
  15.  
  16. NSSortDescriptor *ageSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
  17.  
  18. NSSortDescriptor *firstNameSort = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
  19.  
  20. fetchRequest.sortDescriptors = @[ageSort, firstNameSort];
  21.  
  22. NSError *requstError = nil;
  23.  
  24. NSArray *persons = [self.managedObjectContext executeFetchRequest:fetchRequest error:&requstError];
  25.  
  26. for (Person *person in persons) {
  27. NSLog(@"First Name = %@", person.firstName);
  28. NSLog(@"Last Name= %@", person.lastName);
  29. NSLog(@"Age = %lu", (unsigned long)[person.age unsignedCharValue]);
  30. }
  31.  
  32. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  33. // Override point for customization after application launch.
  34. self.window.backgroundColor = [UIColor whiteColor];
  35. [self.window makeKeyAndVisible];
  36. return YES;
  37. }
  38.  
  39. -(BOOL) deletePerson:(NSArray*)praramPersons
  40. {
  41. BOOL isDelete = NO;
  42.  
  43. if ([praramPersons count] > 0) {
  44. Person *lastPerson = [praramPersons lastObject];
  45. [self.managedObjectContext deleteObject:lastPerson];
  46.  
  47. NSError *savingError = nil;
  48. if ([self.managedObjectContext save:&savingError]) {
  49. NSLog(@"Successfully deleted the last Person in the array");
  50. } else {
  51. NSLog(@"Failed to delete the last Person in the array");
  52. }
  53. } else {
  54. NSLog(@"Could not find any Person entities in the context");
  55. }
  56.  
  57. return isDelete;
  58. }
  59.  
  60. -(BOOL) createPersonSuccess:(NSArray*)paramPersons
  61. {
  62. BOOL createResult = NO;
  63.  
  64. if ([paramPersons count] > 0) {
  65. createResult = YES;
  66. NSUInteger counter = 1;
  67. for (Person *thisPerson in paramPersons) {
  68. NSLog(@"Person %lu First Name = %@", (unsigned long)counter, thisPerson.firstName);
  69. NSLog(@"Person %lu lastName = %@", (unsigned long)counter, thisPerson.lastName);
  70. NSLog(@"Person %lu Age = %ld", (unsigned long)counter, (unsigned long)[thisPerson.age unsignedIntegerValue]);
  71. counter ++;
  72. }
  73. } else {
  74. NSLog(@"Could not find any Person entities in the context");
  75. }
  76.  
  77. return createResult;
  78. }
  79.  
  80. -(void)createNewPerson
  81. {
  82. Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
  83.  
  84. if (newPerson != nil) {
  85. newPerson.firstName = @"Anthony";
  86. newPerson.lastName = @"Robbins";
  87. newPerson.age = @51;
  88.  
  89. NSError *savingError = nil;
  90.  
  91. if ([self.managedObjectContext save:&savingError]) {
  92. NSLog(@"Successfully saved the context.");
  93. } else {
  94. NSLog(@"Failed to save the context. Error = %@", savingError);
  95. }
  96. } else {
  97. NSLog(@"Failed to create the new Person.");
  98. }
  99.  
  100. }
  101.  
  102. -(BOOL) createNewPersonWithFirstName:(NSString*)paramFirstName
  103. lastName:(NSString*)paramLastName
  104. age:(NSUInteger)paramAge
  105. {
  106. BOOL result = NO;
  107.  
  108. if ([paramFirstName length] == 0 | [paramLastName length] == 0) {
  109. NSLog(@"First and Last names are mandatory");
  110. return NO;
  111. }
  112.  
  113. Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
  114.  
  115. if (newPerson == nil) {
  116. NSLog(@"Failed to create the new person.");
  117. return NO;
  118. }
  119.  
  120. newPerson.firstName = paramFirstName;
  121. newPerson.lastName = paramLastName;
  122. newPerson.age = @(paramAge);
  123.  
  124. NSError *savingError = nil;
  125.  
  126. if ([self.managedObjectContext save:&savingError]) {
  127. return YES;
  128. } else {
  129. NSLog(@"Failed to save the new person.Error = %@", savingError);
  130. }
  131. return result;
  132. }

  NSlog

  1. 2014-09-13 00:20:18.452 CoreDataDemo[683:60b] First Name = Anthony
  2. 2014-09-13 00:20:18.453 CoreDataDemo[683:60b] Last Name= Robbins
  3. 2014-09-13 00:20:18.454 CoreDataDemo[683:60b] Age = 51
  4. 2014-09-13 00:20:18.454 CoreDataDemo[683:60b] First Name = Anthony
  5. 2014-09-13 00:20:18.454 CoreDataDemo[683:60b] Last Name= Robbins
  6. 2014-09-13 00:20:18.455 CoreDataDemo[683:60b] Age = 51
  7. 2014-09-13 00:20:18.455 CoreDataDemo[683:60b] First Name = Anthony
  8. 2014-09-13 00:20:18.455 CoreDataDemo[683:60b] Last Name= Robbins
  9. 2014-09-13 00:20:18.456 CoreDataDemo[683:60b] Age = 51
  10. 2014-09-13 00:20:18.456 CoreDataDemo[683:60b] First Name = Anthony
  11. 2014-09-13 00:20:18.456 CoreDataDemo[683:60b] Last Name= Robbins
  12. 2014-09-13 00:20:18.457 CoreDataDemo[683:60b] Age = 51
  13. 2014-09-13 00:20:18.457 CoreDataDemo[683:60b] First Name = Anthony
  14. 2014-09-13 00:20:18.457 CoreDataDemo[683:60b] Last Name= Robbins
  15. 2014-09-13 00:20:18.458 CoreDataDemo[683:60b] Age = 51
  16. 2014-09-13 00:20:18.458 CoreDataDemo[683:60b] First Name = Anthony
  17. 2014-09-13 00:20:18.458 CoreDataDemo[683:60b] Last Name= Robbins
  18. 2014-09-13 00:20:18.459 CoreDataDemo[683:60b] Age = 51
  19. 2014-09-13 00:20:18.459 CoreDataDemo[683:60b] First Name = Anthony
  20. 2014-09-13 00:20:18.485 CoreDataDemo[683:60b] Last Name= Robbins
  21. 2014-09-13 00:20:18.486 CoreDataDemo[683:60b] Age = 51
  22. 2014-09-13 00:20:18.487 CoreDataDemo[683:60b] First Name = Richard
  23. 2014-09-13 00:20:18.487 CoreDataDemo[683:60b] Last Name= Branson
  24. 2014-09-13 00:20:18.487 CoreDataDemo[683:60b] Age = 61
  25. 2014-09-13 00:20:18.488 CoreDataDemo[683:60b] First Name = Richard
  26. 2014-09-13 00:20:18.488 CoreDataDemo[683:60b] Last Name= Branson
  27. 2014-09-13 00:20:18.489 CoreDataDemo[683:60b] Age = 61
  28. 2014-09-13 00:20:18.489 CoreDataDemo[683:60b] First Name = Richard
  29. 2014-09-13 00:20:18.489 CoreDataDemo[683:60b] Last Name= Branson
  30. 2014-09-13 00:20:18.490 CoreDataDemo[683:60b] Age = 61
  31. 2014-09-13 00:20:18.490 CoreDataDemo[683:60b] First Name = Richard
  32. 2014-09-13 00:20:18.491 CoreDataDemo[683:60b] Last Name= Branson
  33. 2014-09-13 00:20:18.491 CoreDataDemo[683:60b] Age = 61
  34. 2014-09-13 00:20:18.495 CoreDataDemo[683:60b] Application windows are expected to have a root view controller at the end of application launch

Objective-c CoreData的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. CoreData教程

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

  3. iphone dev 入门实例4:CoreData入门

    The iPhone Core Data Example Application The application developed in this chapter will take the for ...

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

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

  5. iOS CoreData 中 objectID 的不变性

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

  6. CoreData __ 基本原理

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

  7. iOS CoreData primitive accessor

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

  8. 初识CoreData与详解

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

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

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

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

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

随机推荐

  1. 在Windows 上安装SQL Server的一些注意事项

    基本来说安装SQL Server 单节点数据库并不是很困难的事情,大多可以通过Next来安装完成.其中要注意以下几点 安装.net3.5 可以参考本Blog的一些安装须知. Windows Serve ...

  2. SQL Server 读取CSV中的数据

    测试: Script: create table #Test ( Name ), Age int, T ) ) BULK INSERT #Test From 'I:\AAA.csv' with( fi ...

  3. ssh-keygen实现免密码登陆

    在 Client 端建立 Public 与 Private Key : 建立的方法真的是简单到不行!直接在 192.168.0.100 这个 Client 上面,以 test2 这个账号,使用 ssh ...

  4. html onclick 传参数

    <a id="j-im" class="jd-im btn-gray gys-im" href="javascript:(0);" o ...

  5. Android Studio:Gradle常用命令

    Android Studio中自带Terminal,可以直接使用gradle命令,不必另开命令窗口,相当方便,下面总结一下常用的命令: 1.查看Gradle版本号      ./gradlew -v  ...

  6. request 路径随笔

    1. 路劲可分为 绝对路径 和 相对路径 2. 绝对路径 (开头带"/") 前端: http://localhost:8080/myWebApp/user/login.jsp /m ...

  7. HTML特殊转义字符对照表

    字符 十进制 转义字符 字符 十进制 转义字符 字符 十进制 转义字符 ? ¡ ¡ Á Á Á á á á ¢ ¢ ¢ Â Â ˆ â â £ £ £ Ã Ã Ã ã ã ã ¤ ¤ ¤ Ä Ä &a ...

  8. linux进程管理之开机启动

    下面用自启动apache为例;自启动脚本:/usr/local/apache2/bin:./apachectl start文件位于/etc/rc.d/init.d下,名为apached, 注意要可执行 ...

  9. 【HDOJ】【4405】Aeroplane chess飞行棋

    概率DP/数学期望 kuangbin总结中的第4题 啊还是求期望嘛……(话说Aeroplane chess这个翻译怎么有种chinglish的赶脚……) 好像有点感觉了…… 首先不考虑直飞的情况: f ...

  10. 2-Highcharts曲线图之折线图

    示例图片,在网上下载一张图片如图:其中数据自定义 引入上节模版配置  在script标签中写代码:具体代码如下   信息将在代码中解释. 分析:“五省收益趋势”是标题: x坐标为[2011年-2016 ...