在使用CoreData从数据库查询数据时都是使用NSFetchRequest类进行查询条件设置。
有以下几种查询方式:
嵌套查询:嵌套查询就是查询子类对象成员的属性,使用谓词中的点语法可以实现。
分页查询:就是分段进行查询。就是使用NSFetchRequest的fetchOffset属性设置起始查询位置,使用fetchLimit属性设置分页查询的最大条数。
模糊查询:起始就是谓词中的ENDSWITH、BEGINSWITH、LIKE、CONTAINS ? 和*的具体使用。
具体代码如下:
//
// ViewController.m
// CoreData的简单使用
//
// Created by apple on 15/11/2.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
#import "Employee.h"
#import "Department.h"
#import <CoreData/CoreData.h>
@interface ViewController ()
@property(nonatomic, strong)NSManagedObjectContext *context;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
1. 创建模型文件(相当于数据库里的一张关系表)
2. 添加实体(与一个对象相对应)
3. 创建实体类(相当于模型)
4. 生成上下文 关联模型文件生成数据库
: 注意:关联的时候,如果本地没有数据库文件,coreData自己会创建。
*/
// 上下文是专门用来管理实体对象的
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
// 关联数据库——参数传为nil,意味着从所有的模板xcdatamodeld进行读取
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
// 持久化存储助理(调度器)
// 持久化,把数据保存到一个文件里,而不是内存
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 告诉Coredata数据库的名称和路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *sqlitePath = [doc stringByAppendingPathComponent:@"company.sqlite"];
NSLog(@"%@", sqlitePath);
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];
context.persistentStoreCoordinator = store;
_context = context;
// [self addEmployee];
[self mohuSearcher];
}
// 数据库的操作 CURD — Create Update Read Delete 对记录进行增删改查
#pragma mark 添加员工
-(void)addEmployee
{
for (int i=0 ; i<15 ; i++) {
Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.context];
emp.name = [NSString stringWithFormat:@"王五%d", i];
emp.height = @(1.75+i);
emp.birthday = [NSDate date];
}
NSError *error;
[self.context save:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
}
}
#pragma mark 模糊查询
-(void)mohuSearcher
{
/**
读取IOS部门的员工
*/
// 1. FetchRequest 抓取请求对象
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
/*模糊查询*/
// 2. 设置查询条件
/*
ENDSWITH 表示以什么结尾
BEGINSWITH 表示以什么开头
CONTAINS 表示是否包含指定字符或字符串
LIKE 模糊查找 * 代表0个或多个字符,?代表一个字符
*/
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH %@", @"10"];
request.predicate = predicate;
// 3.设置排序, 按身高的降序排序
NSSortDescriptor *heightSort = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:NO];
request.sortDescriptors = @[heightSort];
// 4. 执行请求
NSError *error = nil;
NSArray *employees = [self.context executeFetchRequest:request error:&error];
if (error) {
NSLog(@"error");
}
for (Employee *emp in employees) {
NSLog(@"名称:%@ 部门%@", emp.name, emp.depart.name);
}
}
#pragma mark 分页查询
-(void)pageSearch
{
/**
读取IOS部门的员工
*/
// 1. FetchRequest 抓取请求对象
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
/**
分页查询 Limit 0~5
*/
// 分页的起始索引
request.fetchOffset = 12; // 表示从下标为0的第一条数据开始
// 分页的条数
request.fetchLimit = 6;
/**
一共15条数据
每次获取6条数据
第一页 0- 5 6条数据
第二页 6- 11 6条数据
第三页 12 - 14 3条数据
*/
// 3.设置排序, 按身高的降序排序
NSSortDescriptor *heightSort = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:NO ];
request.sortDescriptors = @[heightSort];
// 4. 执行请求
NSError *error = nil;
NSArray *employees = [self.context executeFetchRequest:request error:&error];
if (error) {
NSLog(@"error");
}
for (Employee *emp in employees) {
NSLog(@"名称:%@ 部门%@", emp.name, emp.depart.name);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
分段调试运行结果如下:
打开沙盒路径
分页查询如下:
2
模糊查询结果如下: