操作过程

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

CoreData和sqlite的区别

  • CoreData是一个框架;sqlite是苹果使用别人开发好的一个动态库,本质是关系型数据库.

  • CoreData是IOS平台下的一个数据持久化的方式;sqlite可以跨平台使用.

实现思路

  •   首先找到CoreData文件夹
  •   创建Person类,并且建立name属性

Command + N 创建

//代码实现

-------ViewController.h

  1. #import "ViewController.h"
  2. //coreData的框架
  3. //导入框架的方式 : <框架名字/头文件名字>
  4. #import <CoreData/CoreData.h>
  5. #import "Person.h"
  6. #import "AppDelegate.h"
  7.  
  8. @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
  9.  
  10. @property (nonatomic, strong) UITableView *tableView;
  11. //临时数据库
  12. @property (nonatomic, strong) NSManagedObjectContext *objectContext;
  13. //数据源数组
  14. @property (nonatomic, strong) NSMutableArray *dataArray;
  15.  
  16. @end
  17.  
  18. @implementation ViewController
  19.  
  20. - (void)viewDidLoad {
  21.  
  22. [super viewDidLoad];
  23.  
  24. self.dataArray = [NSMutableArray array];
  25. //获取到我们最开始使用的AppDelegate
  26. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  27. self.objectContext = app.managedObjectContext;
  28.  
  29. //调用查询数据的方法
  30. [self searchAll];
  31.  
  32. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
  33. [self.view addSubview:_tableView];
  34. _tableView.delegate = self;
  35. _tableView.dataSource = self;
  36. [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
  37.  
  38. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(barButtonItemClicked)];
  39.  
  40. }
  41.  
  42. - (void)barButtonItemClicked
  43. {
  44. //NSEntityDescription : 实体描述类,通过类方法创建
  45. //第一个参数 : 表示这个实体描述类描述的是哪个实体 (表名)
  46. //第二个参数 : 表示的是在context里边创建一个描述,告诉context我要往里面插入一个object了
  47. NSEntityDescription *description = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.objectContext];
  48. //创建一个实体类
  49. //第一个参数 : 实体描述
  50. //第二个参数 : 在context里面放入这个类
  51. Person *person = [[Person alloc] initWithEntity:description insertIntoManagedObjectContext:self.objectContext];
  52.  
  53. int number = arc4random() % ;
  54. person.name = [NSString stringWithFormat:@"%d号李四", number];
  55.  
  56. [self insertObject:person];
  57.  
  58. }
  59.  
  60. //向coreData中插入一条数据
  61. - (void)insertObject:(Person *)person
  62. {
  63.  
  64. NSError *error = nil;
  65. //把context保存到本地
  66. //这个方法执行之后, 本地数据才发生了改变
  67. [self.objectContext save:&error];
  68.  
  69. if (error == nil) {
  70. [self.dataArray addObject:person];
  71. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataArray.count - inSection:];
  72. [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
  73. }
  74. }
  75.  
  76. //搜索全部
  77. - (void)searchAll
  78. {
  79. //创建一个查询操作, 查询哪个表里面的内容
  80. NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
  81.  
  82. //接收查询数据
  83. NSError *error = nil;
  84. //让context去执行查询操作, 并且返回一个结果数组
  85. NSArray *array = [self.objectContext executeFetchRequest:request error:&error];
  86.  
  87. //判断error是否为nul
  88. if (error == nil) {
  89. [self.dataArray setArray:array];
  90.  
  91. [self.tableView reloadData];
  92. }else
  93. {
  94. NSLog(@"查询失败 ");
  95. }
  96. }
  97.  
  98. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  99. {
  100. //判断一下当前的编辑状态
  101. if (editingStyle == UITableViewCellEditingStyleDelete) {
  102. //获取到想要删除的那条数据
  103. Person *person = self.dataArray[indexPath.row];
  104. //在context中将这条数据删除
  105. [self.objectContext deleteObject:person];
  106.  
  107. NSError *error = nil;
  108.  
  109. [self.objectContext save:&error];
  110.  
  111. if (error == nil) {
  112. //先删数据
  113. [self.dataArray removeObject:person];
  114. //然后更新UI
  115. [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
  116. }
  117. }
  118. }
  119.  
  120. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. //知道要改谁
  123. Person *person = self.dataArray[indexPath.row];
  124.  
  125. int number = arc4random() % ;
  126. person.name = [NSString stringWithFormat:@"%d号张三", number];
  127.  
  128. NSError *error = nil;
  129.  
  130. [self.objectContext save:&error];
  131.  
  132. if (error == nil) {
  133. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
  134. }
  135. }
  136.  
  137. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  138. {
  139. return self.dataArray.count;
  140. }
  141. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
  144. Person *person = self.dataArray[indexPath.row];
  145. cell.textLabel.text = person.name;
  146.  
  147. return cell;
  148. }
  149. - (void)didReceiveMemoryWarning {
  150. [super didReceiveMemoryWarning];
  151. // Dispose of any resources that can be recreated.
  152. }
  153.  
  154. @end

CoreData __ 基本原理的更多相关文章

  1. Ognl表达式基本原理和使用方法

    Ognl表达式基本原理和使用方法 1.Ognl表达式语言 1.1.概述 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,他是一个 ...

  2. Android自定义控件之基本原理

    前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...

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

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

  4. HMM基本原理及其实现(隐马尔科夫模型)

    HMM(隐马尔科夫模型)基本原理及其实现 HMM基本原理 Markov链:如果一个过程的“将来”仅依赖“现在”而不依赖“过去”,则此过程具有马尔可夫性,或称此过程为马尔可夫过程.马尔可夫链是时间和状态 ...

  5. 动态令牌-(OTP,HOTP,TOTP)-基本原理

    名词解释和基本介绍 OTP 是 One-Time Password的简写,表示一次性密码. HOTP 是HMAC-based One-Time Password的简写,表示基于HMAC算法加密的一次性 ...

  6. iOS CoreData 中 objectID 的不变性

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

  7. ZooKeeper基本原理

    ZooKeeper简介 ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等. ZooKeeper设计目的 1. ...

  8. iOS CoreData primitive accessor

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

  9. 初识CoreData与详解

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

随机推荐

  1. mysql主从复制操作步骤

    注: .做主从复制的两台服务器server-id不能相同 .主从的字符集要一样,不然数据导入会报错 .开启db01的log-bin功能 [root@db01 mysql]# vim /etc/my.c ...

  2. ios 快速审核

    https://developer.apple.com/contact/app-store/?topic=expedite

  3. Anaconda 安装 ml_metrics package

    ml_metrics is the Python implementation of Metrics implementations a library of various supervised m ...

  4. JAVA连接SqlServer2008R2和MySql数据库

    问题描述: 下面是有关连接SqlServer2008R2和MySql数据库的封装类 package com.test; import java.sql.Connection; import java. ...

  5. 10Spring高级----青软S2SH(笔记)

  6. js像素运算问题

    通过DOM获取的某一距离属性(比如left)是带px单位的,直接对其算术运算不起效,要先把获取到的值处理一下去掉px单位. 方法一(用处理字符串的方式去除px): var x=document.get ...

  7. KMP模板

    参考:http://www.cnblogs.com/c-cloud/p/3224788.html #include<stdio.h> #include<string.h> vo ...

  8. spring mvc controller间跳转 重定向 传参

    http://blog.csdn.net/jackpk/article/details/19121777/

  9. Linux SVN 命令详解

    1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)   例如:svn checkout svn://192.168.1.1/pro/domain    ...

  10. word20161224

    V.34 V.90 validation / 验证 value entry / 值项 variable / 变量 variable bit rate, VBR / 可变传输率 VBR, variabl ...