CoreData 增删改查
#pragma mark - Core Data Methods
- (void)insertObjectWithFileName:(NSString *)fileName
{
/**
SQL新增记录的过程
1. 拼接一个INSERT的SQL语句
2. 执行SQL
*/
// 1. 实例化并让context“准备”将一条个人记录增加到数据库
ReaderDocument *document = [NSEntityDescription insertNewObjectForEntityForName:kOAPDFDocumentinManagedObjectContext:self.managedObjectContext];
// 2. 设置个人信息
document.fileName = fileName;
// 3. 保存(让context保存当前的修改)
if ([self.managedObjectContext save:nil]) {
NSLog(@"新增成功");
} else {
NSLog(@"新增失败");
}
}
- (NSMutableArray *)getObjectsWithPredicate:(NSString *)predicate
{
// 1. 实例化一个查询(Fetch)请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kOAPDFDocument];
// 3. 条件查询,通过谓词来实现的
// request.predicate = [NSPredicate predicateWithFormat:@"age < 60 && name LIKE '*五'"];
// 在谓词中CONTAINS类似于数据库的 LIKE '%王%'
// request.predicate = [NSPredicate predicateWithFormat:@"phoneNo CONTAINS '1'"];
// 如果要通过key path查询字段,需要使用%K
// request.predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS '1'", @"phoneNo"];
// 直接查询字表中的条件
// 2. 让_context执行查询数据
NSArray *array = [self.managedObjectContext executeFetchRequest:request error:nil];
// for (OAPDFDocument *pdf in array) {
// NSLog(@"\nfielName:%@ \nfilePath:%@ \nfileSize:%@", pdf.fileName, pdf.filePath, pdf.fileSize);
// 在CoreData中,查询是懒加载的
// 在CoreData本身的SQL查询中,是不使用JOIN的,不需要外键
// 这种方式的优点是:内存占用相对较小,但是磁盘读写的频率会较高
// for (Book *b in p.books) {
// NSLog(@"%@ %@ %@", b.name, b.price, b.author);
// }
// }
// for (Book *b in array) {
// NSLog(@"%@ %@ %@", b.name, b.price, b.author);
// }
return [NSMutableArray arrayWithArray:array];
}
- (void)editObjectsWithPredicate:(NSPredicate *)predicate withState:(NSNumber *)state
{
// 1. 实例化一个查询(Fetch)请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kOAPDFDocument];
// 2. 条件查询,通过谓词来实现的
request.predicate = predicate;
// 在谓词中CONTAINS类似于数据库的 LIKE '%王%'
// request.predicate = [NSPredicate predicateWithFormat:@"phoneNo CONTAINS '1'"];
// 如果要通过key path查询字段,需要使用%K
// request.predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS '1'", @"phoneNo"];
// 直接查询字表中的条件
// 3. 让_context执行查询数据
NSArray *array = [self.managedObjectContext executeFetchRequest:request error:nil];
for (ReaderDocument *pdf in array) {
// 3.1修改公文阅读状态
pdf.fileTag = state;
// 3.2修改公文最新打开日期
NSFileManager* fileMngr = [NSFileManager defaultManager];
NSDictionary* attributes = [fileMngr attributesOfItemAtPath:pdf.fileURL error:nil];
pdf.lastOpen = (NSDate *)[attributes objectForKey:NSFileModificationDate];
// 3.3获取并保存,该文件的首页缩略图
UIImage *thumbImage = [pdf imageFromPDFWithDocumentRef:pdf.fileURL];
pdf.thumbImage = UIImagePNGRepresentation(thumbImage);
[self.collectionView reloadData];
break;
}
// 4. 通知_context修改数据是否成功
if ([self.managedObjectContext save:nil]) {
NSLog(@"修改成功");
} else {
NSLog(@"修改失败");
}
}
- (void)removeObjectsWithPredicate:(NSString *)predicate
{
// 1. 实例化查询请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kOAPDFDocument];
// 2. 设置谓词条件
// request.predicate = [NSPredicate predicateWithFormat:@"name = '张老头'"];
request.predicate = [NSPredicate predicateWithFormat:predicate];
// 3. 由上下文查询数据
NSArray *result = [self.managedObjectContext executeFetchRequest:request error:nil];
// 4. 输出结果
for (ReaderDocument *pdf in result) {
// 删除一条记录
[self.managedObjectContext deleteObject:pdf];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pdf.filePath];
if (fileExists) {
[self removeFileWithName:pdf.fileName];
}else{
NSLog(@"File:%@ is not exist!",pdf.fileName);
}
// break;
}
// 5. 通知_context保存数据
if ([self.managedObjectContext save:nil]) {
NSLog(@"删除%lu文件成功",(unsigned long)[result count]);
} else {
NSLog(@"删除失败");
}
}
- (void)removeFileWithName:(NSString *)fileName
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
NSError *error;
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
NSLog(@"Remove fiel:%@ Success!",fileName);
}
else
{
NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
}
}
CoreData 增删改查的更多相关文章
- iOS CoreData 增删改查详解
最近在学习CoreData, 因为项目开发中需要,特意学习和整理了一下,整理出来方便以后使用和同行借鉴.目前开发使用的Swift语言开发的项目.所以整理出来的是Swift版本,OC我就放弃了. 虽然S ...
- IOS - CoreData 增删改查
#pragma mark - Core Data Methods - (void)insertObjectWithFileName:(NSString *)fileName { /** SQL新增记录 ...
- IOS之分析网易新闻存储数据(CoreData的使用,增删改查)
用过网易新闻客户端的朋友们都知道,获取新闻列表时有的时候他会请求网络有时候不会,查看某条新闻的时候再返回会标注已经查看的效果,接下来分析一下是如何实现的. 首先: 1.网易新闻用CoreData存储了 ...
- CoreData的简单使用(二)数据的增删改查,轻量级的版本迁移
上一篇中我们已经使用CoreData创建了一个SQLite数据库 CoreData的简单使用(一)数据库的创建 现在对数据库进行数据的CRUD(增删改查) 1.Data Model 的设置 创建一个D ...
- iOS CoreData (一) 增删改查
代码地址如下:http://www.demodashi.com/demo/11041.html Core Data是iOS5之后才出现的一个框架,本质上是对SQLite的一个封装,它提供了对象-关系映 ...
- CoreData 从入门到精通(二) 数据的增删改查
在上篇博客中,讲了数据模型和 CoreData 栈的创建,那下一步就是对数据的操作了.和数据库一样,CoreData 里的操作也无非是增删改查.下面我们将逐步讲解在 CoreData 中进行增删改查的 ...
- IOS-CoreData(增删改查、表关联、分页和模糊查询、多个数据库)
1>什么是CoreData Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数 ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- ASP.NET从零开始学习EF的增删改查
ASP.NET从零开始学习EF的增删改查 最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...
随机推荐
- eclipse无法启动
-startupplugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar-showsplashorg.eclipse.platfo ...
- StatefulSet: Kubernetes 中对有状态应用的运行和伸缩
在最新发布的 Kubernetes 1.5 我们将过去的 PetSet 功能升级到了 Beta 版本,并重新命名为StatefulSet.除了依照社区民意改了名字之外,这一 API 对象并没有太大变化 ...
- C++11学习
转自: https://www.cnblogs.com/llguanli/p/8732481.html Boost教程: http://zh.highscore.de/cpp/boost/ 本章目的: ...
- C/C++ 获取目录下的文件列表信息
在C/C++编程时,需要获取目录下面的文件列表信息. 1.数据结构 struct dirent { long d_ino; /* inode number 索引 ...
- HttpWebRequest的使用
HttpWebRequest类主要利用HTTP 协议和服务器交互,通常是通过 GET 和 POST 两种方式来对数据进行获取和提交.下面对这两种方式进行一下说明: GET 方式 GET 方式通过在网络 ...
- Socket.IO介绍:支持WebSocket、用于WEB端的即时通讯的框架
一.基本介绍 WebSocket是HTML5的一种新通信协议,它实现了浏览器与服务器之间的双向通讯.而Socket.IO是一个完全由JavaScript实现.基于Node.js.支持WebSocket ...
- IOS中的手势详解
1.点击 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selecto ...
- Mac下如何进行端口转发,方便一系列需要使用80端口进行的调试工作
上篇文章介绍到,可以在本地hosts文件中添加一条记录将微信公众号中的可信域名解析道本地127.0.0.1,但tomcat在Mac下非root权限80端口是启动不了的,所以我们可以利用pfctl端口转 ...
- IDEA报错Target level '1.6' is incompatible with source level '1.7'
解决IDEA 编译级别 Error:java: Target level '1.6' is incompatible with source level '1.7'. A target level ' ...
- Chrome浏览器内嵌的各种手机模拟器
打开chrome的控制台标签,然后,点击simulator子标签页,选择需要的手机即可,如下图: 模拟器如下: 阅读原文:Chrome浏览器内嵌的各种手机模拟器