#import "ViewController.h"
#import "Husband.h"
#import "Wife.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark - 上下文
// 从应用程序包中加载模型文件
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
// 传入模型对象,初始化NSPersistentStoreCoordinator
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 初始化上下文
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
// 设置上下文的persistentStoreCoordinator属性
context.persistentStoreCoordinator = psc;
#pragma mark - 持久化存储库
// 构建SQLite数据库文件的路径
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"model.data"];
// 将数据库路径转成URL
NSURL *url = [NSURL fileURLWithPath:filePath];
// 添加持久化存储库,这里使用SQLite作为存储库
NSError *error = nil;
NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error];
// 判断数据库是否添加成功
if (store == nil) {
[NSException raise:@"添加数据库错误" format:@"%@", [error localizedDescription]];
}
@end
#pragma mark - 添加数据
// 创建一个Husband实体对象,传入上下文
Husband *husband = [NSEntityDescription insertNewObjectForEntityForName:@"Husband" inManagedObjectContext:context];
// 通过键值编码的方式设置Husband的name属性
[husband setValue:@"jack" forKey:@"name"];
// 通过coredata生成的实体类来创建一个Wife实体对象,传入上下文
Wife *wife = [NSEntityDescription insertNewObjectForEntityForName:@"Wife" inManagedObjectContext:context];
// 通过setter方法设置属性
wife.name = @"rose";
// 设置Husband和Wife之间的关联关系(一方关联,另一方自动关联)
wife.husband = husband;
// 利用上下文对象,将数据同步到持久化存储库
BOOL success = [context save:&error];
if (!success) {
[NSException raise:@"访问数据库错误" format:@"%@", [error localizedDescription]];
}
#pragma mark - 删除数据
// 初始化一个查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// 设置要查询的实体
request.entity = [NSEntityDescription entityForName:@"Husband" inManagedObjectContext:context];
// 设置条件过滤(搜索name等于jack2的实体)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"jack2"];
request.predicate = predicate;
// 执行请求,返回一个数组
NSArray *objs = [context executeFetchRequest:request1 error:&error];
if (error) {
[NSException raise:@"查询错误" format:@"%@", [error localizedDescription]];
}
#pragma mark - 查询数据
// 初始化一个查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// 设置要查询的实体
request.entity = [NSEntityDescription entityForName:@"Husband" inManagedObjectContext:context];
// 设置排序(按照name降序)
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
request.sortDescriptors = [NSArray arrayWithObject:sort];
// 设置条件过滤(搜索name中包含字符串"ja"的记录)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@", @"*ja*"];
request.predicate = predicate;
// 执行请求,返回一个数组
NSArray *objs = [context executeFetchRequest:request error:&error];
if (error) {
[NSException raise:@"查询错误" format:@"%@", [error localizedDescription]];
}
// 遍历数据
for (NSManagedObject *obj in objs) {
NSLog(@"name = %@", [obj valueForKey:@"name"]);
// 实体属性中包含另一个实体,不需要再次设置查询请求,Core Data会根据关联关系查询到关联的实体信息
NSLog(@"wife = %@", [[obj valueForKey:@"wife"] valueForKey:@"name"]);
}
转载于:宏创学院