CoreData __ 基本原理
操作过程
- Context想要获取值,先要告诉连接器,我要什么东西
- 链接器再告诉store, 你给我什么东西, store去找
- 找到之后返回给链接器,链接器再返回给Context
CoreData和sqlite的区别
CoreData是一个框架;sqlite是苹果使用别人开发好的一个动态库,本质是关系型数据库.
CoreData是IOS平台下的一个数据持久化的方式;sqlite可以跨平台使用.
实现思路
- 首先找到CoreData文件夹
- 创建Person类,并且建立name属性
Command + N 创建
//代码实现
-------ViewController.h
#import "ViewController.h"
//coreData的框架
//导入框架的方式 : <框架名字/头文件名字>
#import <CoreData/CoreData.h>
#import "Person.h"
#import "AppDelegate.h" @interface ViewController ()<UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView;
//临时数据库
@property (nonatomic, strong) NSManagedObjectContext *objectContext;
//数据源数组
@property (nonatomic, strong) NSMutableArray *dataArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.dataArray = [NSMutableArray array];
//获取到我们最开始使用的AppDelegate
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
self.objectContext = app.managedObjectContext; //调用查询数据的方法
[self searchAll]; _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(barButtonItemClicked)]; } - (void)barButtonItemClicked
{
//NSEntityDescription : 实体描述类,通过类方法创建
//第一个参数 : 表示这个实体描述类描述的是哪个实体 (表名)
//第二个参数 : 表示的是在context里边创建一个描述,告诉context我要往里面插入一个object了
NSEntityDescription *description = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.objectContext];
//创建一个实体类
//第一个参数 : 实体描述
//第二个参数 : 在context里面放入这个类
Person *person = [[Person alloc] initWithEntity:description insertIntoManagedObjectContext:self.objectContext]; int number = arc4random() % ;
person.name = [NSString stringWithFormat:@"%d号李四", number]; [self insertObject:person]; } //向coreData中插入一条数据
- (void)insertObject:(Person *)person
{ NSError *error = nil;
//把context保存到本地
//这个方法执行之后, 本地数据才发生了改变
[self.objectContext save:&error]; if (error == nil) {
[self.dataArray addObject:person];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataArray.count - inSection:];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
}
} //搜索全部
- (void)searchAll
{
//创建一个查询操作, 查询哪个表里面的内容
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"]; //接收查询数据
NSError *error = nil;
//让context去执行查询操作, 并且返回一个结果数组
NSArray *array = [self.objectContext executeFetchRequest:request error:&error]; //判断error是否为nul
if (error == nil) {
[self.dataArray setArray:array]; [self.tableView reloadData];
}else
{
NSLog(@"查询失败 ");
}
} - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//判断一下当前的编辑状态
if (editingStyle == UITableViewCellEditingStyleDelete) {
//获取到想要删除的那条数据
Person *person = self.dataArray[indexPath.row];
//在context中将这条数据删除
[self.objectContext deleteObject:person]; NSError *error = nil; [self.objectContext save:&error]; if (error == nil) {
//先删数据
[self.dataArray removeObject:person];
//然后更新UI
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
}
}
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//知道要改谁
Person *person = self.dataArray[indexPath.row]; int number = arc4random() % ;
person.name = [NSString stringWithFormat:@"%d号张三", number]; NSError *error = nil; [self.objectContext save:&error]; if (error == nil) {
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
}
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
Person *person = self.dataArray[indexPath.row];
cell.textLabel.text = person.name; return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
CoreData __ 基本原理的更多相关文章
- Ognl表达式基本原理和使用方法
Ognl表达式基本原理和使用方法 1.Ognl表达式语言 1.1.概述 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,他是一个 ...
- Android自定义控件之基本原理
前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...
- iOS基本数据库存储方式 - CoreData
CoreData 创建模型文件的过程 1.选择模板 2.添加实体 3.添加实体的属性[注意]属性的首字母必须小写 一.CoreData管理类(必备以下三个类对象) 1.CoreData数据操作的上下文 ...
- HMM基本原理及其实现(隐马尔科夫模型)
HMM(隐马尔科夫模型)基本原理及其实现 HMM基本原理 Markov链:如果一个过程的“将来”仅依赖“现在”而不依赖“过去”,则此过程具有马尔可夫性,或称此过程为马尔可夫过程.马尔可夫链是时间和状态 ...
- 动态令牌-(OTP,HOTP,TOTP)-基本原理
名词解释和基本介绍 OTP 是 One-Time Password的简写,表示一次性密码. HOTP 是HMAC-based One-Time Password的简写,表示基于HMAC算法加密的一次性 ...
- iOS CoreData 中 objectID 的不变性
关于 CoreData的 objectID 官方文档有这样的表述:新建的Object还没保存到持久化存储上,那么它的objectID是临时id,而保存之后,就是持久化的id,不会再变化了. 那么,我想 ...
- ZooKeeper基本原理
ZooKeeper简介 ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等. ZooKeeper设计目的 1. ...
- iOS CoreData primitive accessor
Given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstNa ...
- 初识CoreData与详解
Core Data数据持久化是对SQLite的一个升级,它是iOS集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类. (1)NSManagedObjectModel(被管 ...
随机推荐
- HTML5学习总结-番外05 移动终端适配
一 viewport 在使用移动端设备浏览网页时,移动端浏览器是直接把整个页面放到一个虚拟的视图里来显示的,通常来说这个虚拟的视图大小会比手机屏幕大,用户可以通过手势操作来平移.缩放这个视图. 如果不 ...
- 移动端Web开发调试之Chrome远程调试(Remote Debugging)
比如手机钉钉调试页面,下面是一位同学整理的链接: http://blog.csdn.net/freshlover/article/details/42528643/ 如果inspect 后,一直空白, ...
- qemu 源码调试
1:下载最新的QEMU源码 git clone https://github.com/qemu/qemu.git 2:对qemu进行编译 ./configure --prefix=/usr --lib ...
- 了解了下spring boot,说一下看法
这段时间比较忙,新项目的事比较多,跟着老大忙前忙后,没准备写博客. 下班地铁上看视频,发现spring boot的公开课,看完后,就准备抒抒情怀: 1.从个人的角度来看,使用spring boot可能 ...
- 关于形变属CGAffineTransform性介绍
CGAffineTransformMakeTranslation每次都是以最初位置的中心点为起始参照 CGAffineTransformTranslate每次都是以传入的transform为起始参照, ...
- 2016icpc大连站总结(呐 如果把这段回忆,起个名字珍藏起来,叫它“宝物”应该很合适吧)
10月15号一早乘飞机去了大连,12点这样到了海事大学,是一所很大的学校,来往的学生有些穿着海军服.然后我们到体育馆领了衣服,就去食堂吃午饭,中间有段小插曲,就是我们队的餐券没领..不过那里的负责人让 ...
- MFC双缓存技术代码
屏蔽背景刷新,在View中添加对WM_ERASEBKGND的响应,直接返回TRUE: BOOL CTEMV1View::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息 ...
- Flask安装过程中“配置虚拟环境”步骤报错,找不到activate.bat
Run virtualenv venv --no-setuptools http://stackoverflow.com/questions/21826859/setting-up-a-virtual ...
- 微信小程序开发工具的数据,配置,日志等目录在哪儿? 怎么找?
原文地址:http://www.wxapp-union.com/portal.php?mod=view&aid=359 本文由本站halfyawn原创:感谢原创者:如有疑问,请在评论内回复 ...
- android gpio口控制
android gpio口控制 GPIO口控制方式是在jni层控制的方式实现高低电平输出,类似linux的控制句柄方式,在linux系统下将每个设备看作一个文件,android系统是基于linux内 ...