【CoreData】分页查询和模糊查询

在CoreData实际使用中,分页查询和模糊查询是必不可少的,接下来演示一下:

首先

// 1.创建模型文件 (相当于一个数据库里的表)

// New File ———— CoreData ———— Data Model ———— Next【CoreData】1.简单地使用


// 2.添加实体 (相当于一张表)

// 找到我们创建的模型文件(xxx.xcdatamodeld,我创建的叫School.xcdatamodeld————Add Entity ———— 添加表内属性(这边我添加2个属性(name,age))

【CoreData】1.简单地使用

// 3.创建实体类

// New File ———— CorData ———— NSManagedobject subclass ————Next(记得选择需要关联的表)这样就会自动生成相应的模型



// 4.生成上下文 关联模型文件生成数据库(关联的时候,如果本地没有数据库文件,CoreData会自己创建)

// 上下文,记得引用CoreData框架

    NSManagedObjectContext *context= [[NSManagedObjectContext alloc]init];

// 上下文关联数据库

// model模型文件参数:mergedModelFromBundles:因为模型文件是资源文件,所以会放到BUNDLES里面,所以只需要写nil就可以了。

    NSManagedObjectModel *model= [NSManagedObjectModel mergedModelFromBundles:nil];

// 持久化存储调度器(持久化:把数据保存在一个文件,而不是放在内存中)

    NSPersistentStoreCoordinator *store=[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

// 告诉CoreData数据库的名字和路径

    NSString *doc= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    

    NSString *sqlitePath=[doc stringByAppendingPathComponent:@"School.sqlite"];

    [store addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nilerror:nil];

    

    context.persistentStoreCoordinator =store;


然后我们来测试一下是否可以写入数据


#pragma mark - 添加

- (void)addStudent

{

   //为了更好地演示查询和分页,我们通过FOR循环来创建多个个学生对象

    for(inti = 0;i<<span style="font-variant-ligatures: no-common-ligatures;color: #272ad8">20; i++)

    {

      Student*stu=[NSEntityDescriptioninsertNewObjectForEntityForName:@"Student"inManagedObjectContext:_context];

       

       if(i/2== 0){

          stu.name= [NSStringstringWithFormat:@"李四%d",i];

       }

       else

       {

          stu.name= [NSStringstringWithFormat:@"张三%d",i];

       }

       

       stu.age= @(1.9+ i);

    }

    

   // 直接保存数据库

    [_contextsave:nil];

    

}


写入数据成功,接下来就是分页查询的使用方法

#pragma mark - 分页查询

- (void)pagination

{

   // 1.FectchRequest 抓取请求对象

   NSFetchRequest *request= [NSFetchRequest fetchRequestWithEntityName:@"Student"];

    

   // 2.分页查询

   // 分页的起始索引

    request.fetchOffset= 0;

   // 每一页条数

    request.fetchLimit= 7;

   //因为我们总共添加了20条数据,所以从0- 7会获取7条信息

   //                         7 - 7也是7条信息

   //                    当从 14- 7 获取的时候就只剩6条信息了

//     3.设置排序

//     身高的升序排序

   NSSortDescriptor *ageSort= [NSSortDescriptor sortDescriptorWithKey:@"age"ascending:YES]; //降序为NO

    request.sortDescriptors= @[ageSort];

    

   // 4.执行请求

    NSArray*stus = [_contextexecuteFetchRequest:requesterror:nil];

    

    for(Student*stu instus)

    {

       NSLog(@"名字:%@身高:%@",stu.name,stu.age);

    }

    

}


#pragma mark - 模糊查询

- (void)dimquery

{

   // 1.FectchRequest 抓取请求对象

   NSFetchRequest *request= [NSFetchRequest fetchRequestWithEntityName:@"Student"];

    

   // 2.设置过滤条件

   // 查找开头也可以用like

   NSPredicate*pre= [NSPredicatepredicateWithFormat:@"nameBEGINSWITH %@",@""];

   // like的功能和上面的BEGINSWITH一样

//    NSPredicate *pre =[NSPredicate predicateWithFormat:@"name like %@", @""];

    

   // 查找 “1”结尾

//    NSPredicate *pre =[NSPredicate predicateWithFormat:@"name ENDSWITH %@", @"1"];

    

   // 查找包含张三2”

//    NSPredicate *pre =[NSPredicate predicateWithFormat:@"name CONTAINS %@",@"张三2"];


    request.predicate= pre;

    

   // 3.设置排序

   // 身高的升序排序

   NSSortDescriptor *ageSort= [NSSortDescriptor sortDescriptorWithKey:@"age"ascending:YES]; //降序为NO

    request.sortDescriptors= @[ageSort];

    

   // 4.执行请求

    NSArray*stus = [_contextexecuteFetchRequest:requesterror:nil];

    

    for(Student*stu instus)

    {

       NSLog(@"名字:%@身高:%@",stu.name,stu.age);

    }


 

}



DEMO下载地址:链接: http://pan.baidu.com/s/13Feay 密码: qyjq

OK,以上就是最常用的几种查询方式,在实际应用中可以穿插使用来达到想要的效果,更多地CoreData使用可在博客中找到。


如果内容有错或有什么问题可以与我联系,转载请注明出处,谢谢!


原文地址:https://www.cnblogs.com/miaomiaoshen/p/5188641.html