#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 增删改查的更多相关文章

  1. iOS CoreData 增删改查详解

    最近在学习CoreData, 因为项目开发中需要,特意学习和整理了一下,整理出来方便以后使用和同行借鉴.目前开发使用的Swift语言开发的项目.所以整理出来的是Swift版本,OC我就放弃了. 虽然S ...

  2. IOS - CoreData 增删改查

    #pragma mark - Core Data Methods - (void)insertObjectWithFileName:(NSString *)fileName { /** SQL新增记录 ...

  3. IOS之分析网易新闻存储数据(CoreData的使用,增删改查)

    用过网易新闻客户端的朋友们都知道,获取新闻列表时有的时候他会请求网络有时候不会,查看某条新闻的时候再返回会标注已经查看的效果,接下来分析一下是如何实现的. 首先: 1.网易新闻用CoreData存储了 ...

  4. CoreData的简单使用(二)数据的增删改查,轻量级的版本迁移

    上一篇中我们已经使用CoreData创建了一个SQLite数据库 CoreData的简单使用(一)数据库的创建 现在对数据库进行数据的CRUD(增删改查) 1.Data Model 的设置 创建一个D ...

  5. iOS CoreData (一) 增删改查

    代码地址如下:http://www.demodashi.com/demo/11041.html Core Data是iOS5之后才出现的一个框架,本质上是对SQLite的一个封装,它提供了对象-关系映 ...

  6. CoreData 从入门到精通(二) 数据的增删改查

    在上篇博客中,讲了数据模型和 CoreData 栈的创建,那下一步就是对数据的操作了.和数据库一样,CoreData 里的操作也无非是增删改查.下面我们将逐步讲解在 CoreData 中进行增删改查的 ...

  7. IOS-CoreData(增删改查、表关联、分页和模糊查询、多个数据库)

    1>什么是CoreData Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数 ...

  8. Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示

    Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...

  9. ASP.NET从零开始学习EF的增删改查

           ASP.NET从零开始学习EF的增删改查           最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...

随机推荐

  1. 日期时间篇asctime ctime gettimeofday gmtime localtime mktime settimeofday time

    asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime,localtime 表头文件 #include<time.h> 定义函数 char * asc ...

  2. [转载] java的书

    1. Java 语言基础 谈到Java 语言基础学习的书籍,大家肯定会推荐Bruce Eckel 的<Thinking in Java >.它是一本写的相当深刻的技术书籍,Java 语言基 ...

  3. 第一章 第一个spring boot程序

    环境: jdk:1.8.0_73 maven:3.3.9 spring-boot:1.2.5.RELEASE(在pom.xml中指定了) 注意:关于spring-boot的支持, 最少使用jdk7(j ...

  4. C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数

    在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...

  5. Git的简单介绍

    每次看到别人写Git的文章,同学中也有用Git感觉很高大上的感觉,工作中用的是SVN,周末倒腾了一下Git,Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 与 ...

  6. 3 Sum leetcode java

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  7. 【Dagger2】 案例大全

    只有Inject是不可以的,必须有Component public class Test { @Inject Person person; private void test() { System.o ...

  8. T-SQL with as 的用法(转) SQL 下的递归查询 SQL2005(CTE) ,SQL2000(Function 递归)

    摘自: http://blog.csdn.net/bluefoxev/article/details/6779794 ------- SQL2005 方法 一.WITH AS的含义     WITH ...

  9. linux主机名的修改

    导读 在一个局域网中,每台机器都有一个主机名,便于主机与主机之间的区分,因此为每台机器设置主机名,以容易记忆的方法来相互访问.比如我们在局域网中可以为根据每台机器的功用来为其命名. 查看主机名命令 [ ...

  10. java编程思想---对象

    一.对象 对于每种语言来说,都有自己操纵内存中元素的方法. 在java中,一切被视为对象.可是操纵对象的是一个"引用".举个样例,能够比作为遥控器对电视的操作,遥控器就是引用,而电 ...