第一步:创建使用CoreData的工程
第二步:创建数据模型,并为数据模型添加属性
第三步:修改CoreData的Codegen属性为Manuel/None
不修改这个属性会报错
第四步:将模型转为代码文件
代码(增删改查)
// 存储的路径URL
- (NSURL *)getDocumentURL
{
return [NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"MySqlite.sqlite"]];
}
// 懒加载
// 管理对象上下文
- (NSManagedObjectContext *)context
{
if (_context == nil) {
_context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
}
[_context setPersistentStoreCoordinator:self.dinator];
return _context;
}
// 持久化存储调度器
- (NSPersistentStoreCoordinator *)dinator
{
if (_dinator == nil) {
_dinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.model];
}
// 添加存储器
/**
* type:一般使用数据库存储方式NSSQLiteStoreType
* configuration:配置信息,一般无需配置
* URL:要保存的路径
* options:参数信息 一般无需设置
*/
// 拼接url路径
NSURL *url = [self getDocumentURL];
// 版本迁移
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption:@(YES),
NSInferMappingModelAutomaticallyOption:@(YES)};
// 设置数据库相关信息
[_dinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:nil];
return _dinator;
}
// 管理对象模型(实体)
- (NSManagedObjectModel *)model
{
if (_model == nil) {
_model = [[NSManagedObjectModel alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Test" withExtension:@"momd"]];
}
return _model;
}
- (void)save
{
[self.context save:nil];
}
- (void)add:(NSString *)name age:(int)age sex:(BOOL)sex
{
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person.name = name;
person.age = age;
person.sex = sex;
[self save];
}
- (void)deleteModel:(NSString *)name age:(int)age sex:(BOOL)sex
{
NSArray *array = [self search:name age:age sex:sex];
// 5.删除数据
[array enumerateObjectsUsingBlock:^(Person * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.context deleteObject:obj];
}];
// 6.同步到数据库
[self save];
}
- (void)change:(NSString *)name age:(int)age sex:(BOOL)sex
{
NSArray *arr = [self search:name age:age sex:sex];
//5.改变数据
[arr enumerateObjectsUsingBlock:^(Person * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.name = name;
obj.sex = sex;
obj.age = age;
}];
[self save];
}
- (NSArray *)search:(NSString *)name age:(int)age sex:(BOOL)sex
{
// 1.创建一个查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 2.创建查询谓词
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",name];
// 3.给查询请求设置谓词
request.predicate = predicate;
// 4.查询数据
NSArray *array = [self.context executeFetchRequest:request error:nil];
return array;
}
- (NSArray *)searchAll
{
// 1.创建一个查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
NSArray *array = [self.context executeFetchRequest:request error:nil];
return array;
}
- (void)deleteAll
{
NSArray *array = [self searchAll];
[array enumerateObjectsUsingBlock:^(Person * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.context deleteObject:obj];
}];
}