突然想起来几年前 我的人生第一个项目,数据持久化全篇使用的CoreData做的,为了纪念,今天找到当年的代码出来,回忆往事。
当时是自己封装了一个handle类 ,把数据库 的增删改查常用操作封装了一下,调用的时候也挺好用的,省得每次都写重复代码,coredata貌似使用的人不多吗,周围同事同行的朋友交流的时候,几乎没有人使用它,这也导致我这个项目交接给下一波同事的时候,经常找我售后服务,讲来讲去,后来的项目再也没用过它了。
关于CoreData的使用网上一堆教程,各位看官可以随意查资料学习!
新建文件coredata操作类:GKCoreDataHandle继承NSObject;
.h文件列出数据持久化常用的增删改查方法:
@interface GKCoreDataHandle : NSObject
// 每次存完数据、修改数据之后执行保存操作
+ (void)saveChangeData;
// 往数据库存(插入)指定实体对象的数据,返回一个实体对象
+ (NSManagedObject*)insertNewObjectFromEntityName:(NSString *)entityName;
// 清除coradata缓存数据的方法- 清楚指定的某个实体对象的数据
+ (void)deleteUserDataFromEntityName:(NSString *)entityName;
// 修改指定实体数据库里面数据(有多条的时候改的最后一条数据)(后期需要指定修改第几条的时候增加下标入参)
+ (void)modifyObjectWithEntityName:(NSString *)entityName replaceData:(id)data;
// 查询SysUser表中的staffId
+ (NSString *)getStaffId;
// 查询coradata缓存数据的方法- 获取查询结果是实体对象数组
+ (NSArray *)fetchCacheDataArrWithEntityName:(NSString *)entityName;
// 查询coradata缓存数据的方法- 获取查询结果是具体的一个实体对象
+ (id)fetchCacheDataObjWithEntityName:(NSString *)entityName;
//查询coradata缓存数据的方法- 通过谓词来查询,返回查询结果的数组
+ (NSArray*)fetchCacheDataArrWithEntityName:(NSString *)entityName andPredicate:(NSPredicate *)predicate;
// 分页查询:传入分页的起始索引fetchOffset 、分页的条数fetchLimit
+ (NSArray*)fetchCacheDataArrWithEntityName:(NSString *)entityName andFetchOffset:(NSInteger)fetchOffset andFetchLimit:(NSInteger)fetchLimit;
@end
.m文件列出具体的方法实现:
#import "GKCoreDataHandle.h"
#import "NSManagedObject_data.h"
@implementation GKCoreDataHandle
#pragma mark - 每次存完数据、修改数据之后执行保存操作
+ (void)saveChangeData {
// 本地用户信息数据
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext;
NSError *error;
if ([context save:&error]) {
NSLog(@"数据添加成功");
} else {
NSLog(@"数据添加失败 %@",error);
}
}
- (NSArray *)fetchRequest:(NSString *)entityName {
// 本地用户信息数据
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
//构造查询对象
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSError* error=nil;
NSArray *fetchResult = [context executeFetchRequest:request error:&error];
if (fetchResult == nil) {
NSLog(@"查询sysuser用户缓存数据失败 Error:%@",error);
return @[];
} else {
return fetchResult;
}
}
#pragma mark - 获取coredata中SysUser表中的staffId
+ (NSString *)getStaffId {
GKCoreDataHandle *gKCoreDataHandle = [[GKCoreDataHandle alloc] init];
NSArray *fetchResult = [gKCoreDataHandle fetchRequest:@"SysUser"];
NSString *staffId = nil;
if (fetchResult != nil) {
for (SysUser* sysUser in fetchResult) {
staffId = sysUser.staffId;
break;
}
} else {
staffId = @"";
}
return staffId;
}
#pragma mark - 查询结果是:实体对象数组
+ (NSArray *)fetchCacheDataArrWithEntityName:(NSString *)entityName {
// 本地用户信息数据
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext; // 获取上下文
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init]; // 构造查询对象
[request setEntity:entity];
NSError* error=nil;
NSArray *fetchResult = [context executeFetchRequest:request error:&error];
if (fetchResult == nil) {
NSLog(@"查询用户缓存数据失败 Error:%@",error);
return @[];
} else {
return fetchResult;
}
}
#pragma mark - 查询结果是:一个实体对象
+ (id)fetchCacheDataObjWithEntityName:(NSString *)entityName {
// 本地用户信息数据
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext; // 获取上下文
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init]; // 构造查询对象
[request setEntity:entity];
NSError* error=nil;
NSArray *fetchResult = [context executeFetchRequest:request error:&error];
if (fetchResult == nil) {
NSLog(@"查询用户缓存数据失败 Error:%@",error);
return @"";
} else {
return fetchResult.lastObject;
}
}
#pragma mark - 通过谓词来查询,返回查询结果的数组
+ (NSArray*)fetchCacheDataArrWithEntityName:(NSString *)entityName andPredicate:(NSPredicate *)predicate {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext; // 获取上下文
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init]; // 构造查询对象
[request setEntity:entity];
request.predicate = predicate; // 查询的条件
NSError* error=nil;
NSArray *fetchResult = [context executeFetchRequest:request error:&error];
if (fetchResult == nil) {
NSLog(@"查询用户缓存数据失败 Error:%@",error);
return @[];
} else {
return fetchResult;
}
}
#pragma mark - 清除指定实体对象的数据
+ (void)deleteUserDataFromEntityName:(NSString *)entityName {
//清除用户数据
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//1. 获得context
NSManagedObjectContext *context = app.managedObjectContext;
[context reset];
NSEntityDescription *description = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setIncludesPropertyValues:NO];
[request setEntity:description];
NSError *error = nil;
NSArray *datas = [context executeFetchRequest:request error:&error];
if (!error && datas && [datas count])
{
for (NSManagedObject *obj in datas)
{
[context deleteObject:obj];
}
if (![context save:&error])
{
NSLog(@"error:%@",error);
}
[context save:nil]; // 删除后 保存空
}
}
#pragma mark - 往数据库存(插入)指定实体对象的数据
+ (NSManagedObject*)insertNewObjectFromEntityName:(NSString *)entityName {
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// 获得context
NSManagedObjectContext *context = app.managedObjectContext;
return [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];;
}
#pragma mark - 修改指定实体数据库里面数据(有多条的时候改的最后一条数据)(后期需要指定修改第几条的时候增加下标入参)
+ (void)modifyObjectWithEntityName:(NSString *)entityName replaceData:(id)data {
if (data) {
NSArray *fetchResult = [self fetchCacheDataArrWithEntityName:entityName];
NSManagedObject *entity = [fetchResult lastObject];
entity.data = data;
[self saveChangeData];
}
}
#pragma mark - 分页查询
+ (NSArray*)fetchCacheDataArrWithEntityName:(NSString *)entityName andFetchOffset:(NSInteger)fetchOffset andFetchLimit:(NSInteger)fetchLimit {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext; // 获取上下文
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init]; // 构造查询对象
[request setEntity:entity];
request.fetchOffset = fetchOffset; // 分页的起始索引
request.fetchLimit = fetchLimit; // 分页的条数
NSError* error=nil;
NSArray *fetchResult = [context executeFetchRequest:request error:&error];
if (fetchResult == nil) {
NSLog(@"查询用户缓存数据失败 Error:%@",error);
return @[];
} else {
return fetchResult;
}
}
@end