#pragma mark - Core Data Methods
- (void)insertObjectWithFileName:(NSString *)fileName
{
/**
SQL新增记录的过程
1. 拼接一个INSERT的SQL语句
2. 执行SQL
*/
// 1. 实例化并让context“准备”将一条个人记录增加到数据库
ReaderDocument *document = [NSEntityDescription insertNewObjectForEntityForName:kOAPDFDocument inManagedObjectContext: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(@" fielName:%@ filePath:%@ fileSize:%@", 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]);
}
}