1. 创建项目
2. 创建CoreDataManager来管理
MTCoreDataManager.h文件
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface MTCoreDataManager : NSObject
// 创建一个管理对象上下文
@property (nonatomic, strong) NSManagedObjectContext *managerContext;
// 单例
+ (instancetype)sharedManager;
@end
MTCoreDataManager.m文件
#import "MTCoreDataManager.h"
@implementation MTCoreDataManager
static MTCoreDataManager *sharedManager;
// 单例
+ (instancetype)sharedManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [MTCoreDataManager new];
});
return sharedManager;
}
// 创建一个管理对象上下文
- (NSManagedObjectContext *)managerContext {
if (_managerContext == nil) {
// 创建对象
_managerContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
// 给上下文设置持久化存储协调器
// 模型文件url
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Person" withExtension:@"momd"];
// 根据url获取到模型文件
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
// 设置模型文件
NSPersistentStoreCoordinator *persitent = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 数据路径放置在沙盒
NSArray *documentArr = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentPath = [[documentArr firstObject] stringByAppendingPathComponent:@"person.db"];
NSLog(@"%@", documentPath);
// 获取文件路径的url
NSURL *pathUrl = [NSURL fileURLWithPath:documentPath];
// 添加数据库文件路径
[persitent addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:pathUrl options:nil error:nil];
// 设置持久化存储协调器
[_managerContext setPersistentStoreCoordinator:persitent];
}
return _managerContext;
}
@end
3. 创建CoreData
右键项目->New File...->Core Data->Data Model->修改类名->点击create.
4. 创建实体类
点击上一步新建的Person.xcdatamodeld文件,选择左下角的Add Entity,接下来自己可修改实体名,并在右侧添加属性。
5. 创建NSManagedObjectModel实体类
右键项目->Core Data->NSManagedObjectObject subclass->选择Person->next->next->create。创建好之后项目结构如下:
6. 插入数据
// 插入
- (IBAction)insert {
// 通过实体描述出实体对象
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[MTCoreDataManager sharedManager].managerContext];
person.name = @"mazaiting";
person.age = @(24);
// 提交数据
[[MTCoreDataManager sharedManager].managerContext save:nil];
NSLog(@"插入成功");
}
7. 查询数据
// 查询
- (IBAction)fetch {
// 查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// 设置参数
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:[MTCoreDataManager sharedManager].managerContext];
// 设置实体
request.entity = entity;
// 谓词--类似于sql中的where
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age = %d", 24];
// 设置谓词
request.predicate = predicate;
// 排序
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
// 设置排序
request.sortDescriptors = @[sort];
// 执行查询请求
NSArray *arr = [[MTCoreDataManager sharedManager].managerContext executeFetchRequest:request error:nil];
if (arr.count == 0) {
NSLog(@"表中无数据");
}
// 打印结果
for (Person *person in arr) {
NSLog(@"name: %@, age: %@", person.name, person.age);
}
}
8. 更新数据
// 更新
- (IBAction)update {
// 创建请求
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// 设置参数
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:[MTCoreDataManager sharedManager].managerContext];
// 设置实体
request.entity = entity;
// 设置谓词
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age = %d", 24];
request.predicate = predicate;
// 执行请求
NSArray *arr = [[MTCoreDataManager sharedManager].managerContext executeFetchRequest:request error:nil];
for (Person *person in arr) {
person.name = @"zaiting";
}
[[MTCoreDataManager sharedManager].managerContext save:nil];
NSLog(@"更新成功");
}
9. 删除数据
// 删除
- (IBAction)delete {
// 创建请求
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// 获取实体
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:[MTCoreDataManager sharedManager].managerContext];
// 设置实体
request.entity = entity;
// 创建谓词
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age = %d", 24];
// 设置谓词
request.predicate = predicate;
// 执行命令
NSArray *arr = [[MTCoreDataManager sharedManager].managerContext executeFetchRequest:request error:nil];
for (Person *person in arr) {
[[MTCoreDataManager sharedManager].managerContext deleteObject:person];
}
NSLog(@"删除成功");
}