/**
* 传入实体类名创建MO实体对象
* @param entityName 实体名称(NSManagerObject类型)
* @return 返回一个MO实体对象
*/
+ (NSManagedObject *)createManagerObject:(NSString *)entityName;
/**
* 添加实体对象
* @param mo 传入一个MO实体
*/
+ (BOOL)addManagerObject:(NSManagedObject *)mo;
/**
* 查询数据
* @param entityName 实体名称(NSManagerObject类型)
* @param predicate 谓词条件
*/
+ (NSArray *)queryDataWithEntityName:(NSString *)entityName andPredicate:(NSPredicate *)predicate;
//修改数据
+ (void)updateMO:(NSManagedObject *)mo;
//删除数据
+ (void)deleteMO:(NSManagedObject *)mo;
static NSManagedObjectContext *_context;
+ (void)initialize {
// 1.取得数据模型文件
NSURL *url = [[NSBundle mainBundle] URLForResource:@"CoreDataModel" withExtension:@"momd"];
// 2.创建加载数据模型对象 并将取得的模型文件交给它
NSManagedObjectModel *loadDataModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
// 3.创建管理本地数据模型的psc对象
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:loadDataModel];
// (1)设置数据库存放路径
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"myData.sqlite"];
// (2)转换成URL
NSURL *urlPath = [NSURL fileURLWithPath:path];
// 4.打开数据库
NSError *error = nil;
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:urlPath options:nil error:&error];
if (error) {
NSLog(@"数据库打开失败");
}else{
NSLog(@"数据库打开成功");
}
// 5.创建托管上下文
_context = [[NSManagedObjectContext alloc] init];
[_context setPersistentStoreCoordinator:psc];
}
//传入实体名 返回一个MO实体对象
+ (NSManagedObject *)createManagerObject:(NSString *)entityName
{
if (entityName.length == 0) {
return nil;
}
NSManagedObject *mo = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:_context];
return mo;
}
//添加实体对象
+ (BOOL)addManagerObject:(NSManagedObject *)mo
{
// 添加一个实体对象
[_context insertObject:mo];
// 保存对象
NSError *error = nil;
[_context save:&error];
if (error) {
NSLog(@"添加数据失败");
return NO;
}else{
NSLog(@"添加数据成功");
return YES;
}
}
//查询数据
+ (NSArray *)queryDataWithEntityName:(NSString *)entityName andPredicate:(NSPredicate *)predicate
{
if (entityName.length == 0) {
return nil;
}
//2.创建查询的请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
<div><span style="font-size: 13px; font-family: 'Microsoft YaHei';">// request.fetchLimit = 5; //设置查询数据的个数</span><span style="font-family: Menlo;">
</span><span style="font-size: 13px; font-family: 'Microsoft YaHei';">// request.fetchOffset = 1; //设置查询数据的起始点</span></div><div><span style="font-family: Menlo;"> </span></div><div><span style="font-size: 13px; font-family: 'Microsoft YaHei';">// 设置数据按照某一个属性的大小排序</span><span style="font-family: Menlo;"></span></div><div><span style="font-size: 13px; font-family: 'Microsoft YaHei';">// NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"userAge" ascending:NO];</span></div><div><span style="font-size: 13px; font-family: 'Microsoft YaHei';">// request.sortDescriptors = @[descriptor];</span></div>
request.predicate = predicate;
//3.查询
NSArray *result = [_context executeFetchRequest:request error:nil];
return result;
}
//修改数据
+ (void)updateMO:(NSManagedObject *)mo
{
if (mo == nil) {
return;
}
if([_context save:nil])
{
NSLog(@"修改成功");
}
}
//删除数据
+ (void)deleteMO:(NSManagedObject *)mo
{
if (mo == nil) {
return;
}
[_context deleteObject:mo];
[_context save:nil];
}