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的增删改查 最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...
随机推荐
- 带你走进EJB--它都有哪些Bean
通过前面一系列EJB的博客,我们已经对EJB有了一个宏观的了解.为够更好的在企业项目中使用EJB,我们很有必要对EJB的一些基本内容进行深入,这次我们主要进行的主题是Enterprise Java B ...
- Reverse Linked List II leetcode java
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1 ...
- 为什么空格拷贝到linux 会变成两个
为什么空格拷贝到linux 会变成两个 学习了:https://zhidao.baidu.com/question/266438357.html 在vi界面内输入:set paste 然后进行拷贝: ...
- what difference between libfm and libffm
https://www.kaggle.com/users/25112/steffen-rendle/forum Congratulations to Yu-Chin, Wei-Sheng, Yong ...
- fatal error C1853: '<filename>' is not a precompiled header file
当编译c和c++混合的项目时,会出现如下类似的错误 fatal error C1853: '<filename>' is not a precompiled header file 解决方 ...
- CF 557B(Pasha and Tea-贪心)
B. Pasha and Tea time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 当Activity继承AppCompatActivity如何实现隐藏标题栏与状态栏
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); g ...
- 应用程序正常初始化(0xc0000135)失败的解决方法
转自:http://blog.sina.com.cn/s/blog_64fba4e00100mzf9.html 这是由于没有安装.NET framework 所造成的,请安装.NET framewor ...
- Android 笔记-Fragment 与 Activity之间传递数据
Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...
- 设定StatusBar的文字成不同的颜色
设定StatusBar上的文字,该文字以StatusBar所在Form的字型设定为准,并以form的ForeColor为字的颜色,文字过长时,自动会截除这个程式的实质意义不太大,因为当文字被盖掉後需自 ...