使用coreData
1、设计数据模型

2、创建持久化视图和控制器
#import "BIDViewController.h"
#import "BIDAppDelegate.h" static NSString * const kLineEntityName = @"Line";
static NSString * const kLineNumberKey = @"lineNumber";
static NSString * const kLineTextKey = @"lineText"; @interface BIDViewController () @property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *lineFields; @end @implementation BIDViewController - (void)viewDidLoad
{
[super viewDidLoad];
//获取应用委托的引用,使用引用获得创建托管对象上下文。
BIDAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext * context = [appDelegate managedObjectContext]; //创建一个获取请求并将实体描述传递给它,以便请求知道要检索的对象类型。
NSFetchRequest * request = [[NSFetchRequest alloc]
initWithEntityName:kLineEntityName];
//通过执行没有谓语的请求,上下文将返回库中的每一个Line对象。
NSError * error;
NSArray * objects = [context executeFetchRequest:request error:&error];
if (objects == nil) {
NSLog(@"There was an error!");
// Do whatever error handling is appropriate
}
//遍历以获取托管对象数组,从中提取每个托管对象的lineNumber和lineText值,并使用信息更新界面的文本框。
for (NSManagedObject * oneObject in objects) {
int lineNum = [[oneObject valueForKey:kLineNumberKey] intValue];
NSString *lineText = [oneObject valueForKey:kLineTextKey]; UITextField *theField = self.lineFields[lineNum];
theField.text = lineText;
} UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
} - (void)applicationWillResignActive:(NSNotification *)notification
{
//先获取对应的委托引用,然后使用此引用获取指向应用的默认上下文指针。
BIDAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext * context = [appDelegate managedObjectContext];
NSError *error;
for (int i = ; i < ; i++) {
UITextField *theField = self.lineFields[i];
//为line实体创建获取请求,创建一个谓语,确认持久存储中是否已经有一个与这个字段对应的托管对象。
NSFetchRequest *request = [[NSFetchRequest alloc]
initWithEntityName:kLineEntityName];
NSPredicate *pred = [NSPredicate
predicateWithFormat:@"(%K = %d)", kLineNumberKey, i];
[request setPredicate:pred];
//在上下文中执行获取请求
NSArray * objects = [context executeFetchRequest:request error:&error];
if (objects == nil) {
NSLog(@"There was an error!");
// Do whatever error handling is appropriate
}
//申明一个指向NSManagedObject的指针并将它设置为nil。检查返回值对象objects,如果存在有效对象就加载,否则创建一个新的托管对象来保存这个字段的文本。
NSManagedObject * theLine = nil;
if ([objects count] > ) {
theLine = [objects objectAtIndex:];
} else {
theLine = [NSEntityDescription
insertNewObjectForEntityForName:kLineEntityName
inManagedObjectContext:context];
}
//使用键——值编码来设置行号以及此托管对象的文本。
[theLine setValue:[NSNumber numberWithInt:i] forKey:kLineNumberKey];
[theLine setValue:theField.text forKey:kLineTextKey];
}
//通知上下文保存修改。
[appDelegate saveContext];
}
使用coreData的更多相关文章
- iOS基本数据库存储方式 - CoreData
CoreData 创建模型文件的过程 1.选择模板 2.添加实体 3.添加实体的属性[注意]属性的首字母必须小写 一.CoreData管理类(必备以下三个类对象) 1.CoreData数据操作的上下文 ...
- iOS CoreData 中 objectID 的不变性
关于 CoreData的 objectID 官方文档有这样的表述:新建的Object还没保存到持久化存储上,那么它的objectID是临时id,而保存之后,就是持久化的id,不会再变化了. 那么,我想 ...
- CoreData __ 基本原理
操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context Co ...
- 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(被管 ...
- CoreData教程
网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...
- CoreData和SQLite多线程访问时的线程安全
关于CoreData和SQLite多线程访问时的线程安全问题 数据库读取操作一般都是多线程访问的.在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱.IOS中常 ...
- IOS数据存储之CoreData使用优缺点
前言: 学习了Sqlite数据之后认真思考了一下,对于已经习惯使用orm数据库的开发者或者对sql语句小白的开发者来说该如何做好数据库开发呢?这个上网搜了一下?看来总李多虑了!apple 提供了一种数 ...
- iOS开发之表视图爱上CoreData
在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功 ...
- CoreData
之前在学习使用SQLite时, 需要编写大量的sql语句,完成数据的增删改查,但对于不熟悉sql语句的开发人员来说,难度较大,调试程序比较困难. 由此出现CoreData框架,将sql的操作转换成为对 ...
随机推荐
- [微软实习生2014]K-th string
很久之前的事情了,微软2014实习生的在线测试题,记录下来以备后用. 题目描述: Description Consider a string set that each of them consist ...
- UIButton 在 iOS7.0与iOS7.1 中关于enabled的一点区别
前些日子,在一台iOS7.0的设备上进行调试,关于UIButton的一部分代码如下 1 self.btn_loadmore.enabled = NO; 2 [self.btn_loadmore set ...
- Windows Server 2012 四个版本对比
Windows Server 2012 有4种版本: Foundation, Essentials, Standard and Datacenter. 版本 Foundation Essentials ...
- Running a Remote Desktop on a Windows Azure Linux VM (远程桌面到Windows Azure Linux )-摘自网络(试了,没成功 - -!)
A complete click-by-click, step-by-step video of this article is available ...
- 咏南CS多层插件式开发框架支持最新的DELPHI XE7
DATASNAP中间件: 中间件已经在好几个实际项目中应用,长时间运行异常稳定,可无人值守: 可编译环境:DELPHI XE5~DELPHI XE7,无需变动代码: 支持传统TCP/IP方式也支持RE ...
- 在线的JSON formate工具
一个非常好的json formate工具 可以很容易发现json的错误,以及对json进行格式化 https://jsonformatter.curiousconcept.com/
- 使用Jena RDF API 开发脚本语言管理资源描述框架模型
摘要 资源描述框架(Resource Description Framework RDF)是一种以XML格式描述元数据的标准格式.Jena是一种用于将关系数据库或是文本文件中所表示的数据建立为元数据模 ...
- ZendFramework2 与MongoDB的整合
从网上找了很多文章,先是直接搜关键字找zf2与mongoDB的文章,然后回到源头先学习了一下mongoDB是什么,以及纯PHP环境下怎么用,又从github上找了几个mongoDB的zf2模块,还FQ ...
- .Net 代码安全保护产品DNGuard HVM使用
前辈人物写的程序啊! 官方网站:http://www.dnguard.net/index.aspx 官方博客:http://www.cnblogs.com/rick/ (很久没更新了) 原文http: ...
- Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...