• CoreData 增删改查


    #pragma mark - Core Data Methods

    - (void)insertObjectWithFileName:(NSString *)fileName

    {

        /**

         SQL新增记录的过程

         1. 拼接一个INSERTSQL语句

         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(@" 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 defaultManagerfileExistsAtPath: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(NSDocumentDirectoryNSUserDomainMaskYESobjectAtIndex: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]);

        }

    }

     

     
     
  • 相关阅读:
    easyUI combobox下拉框很长,easyUI combobox下拉框如何显示滚动条的解决方法
    一步步实现 easyui datagrid表格宽度自适应,效果非常好
    javascript数据结构与算法--基本排序算法(冒泡、选择、排序)及效率比较
    javascript数据结构与算法--二叉树遍历(后序)
    javascript数据结构与算法--二叉树遍历(先序)
    javascript数据结构与算法--二叉树遍历(中序)
    javascript数据结构与算法--二叉树(插入节点、生成二叉树)
    散列表,散列函数,碰撞处理解决:线性探测法
    thinkphp5 Windows下用Composer引入官方GitHub扩展包
    thinkphp5.0开发规范
  • 原文地址:https://www.cnblogs.com/fengmin/p/5015566.html
Copyright © 2020-2023  润新知