LKDBHelper是對FMDB的封裝,可以在不寫sql語句的情況下,使用model就可以全自動的進(jìn)行數(shù)據(jù)表的創(chuàng)建,及數(shù)據(jù)的增、刪、改、查。
MagicalRecord是對CoreData的簡單封裝
引入方式cocoachina:
pod 'MagicalRecord', '~> 2.3.2'
pod 'LKDBHelper', '~> 2.5.1'
LKDBHelper初始化
LKDBModel是我需要存儲的類,.h文件:
@property (nonatomic , assign) NSInteger model_id;
@property (nonatomic , strong) NSString *name;
@property (nonatomic , assign) BOOL isShow;
@property (nonatomic , strong) NSArray *dataArray;
//由于發(fā)現(xiàn)字典類型不能直接存儲,所以轉(zhuǎn)成NSString進(jìn)行存儲。
@property (nonatomic , strong) NSString *dataDictString;
.m文件,首先引入#import "LKDBHelper.h"頭文件,一般情況,設(shè)置一個主鍵名就行:
///**設(shè)置表名,一般不寫,默認(rèn)表名為類名*/
//+(NSString *)getTableName
//{
// return @"LKDB.db";
//}
/**主鍵名*/
+(NSString *)getPrimaryKey
{
return @"model_id";
}
/**復(fù)合主鍵*/
//+(NSArray *)getPrimaryKeyUnionArray
//{
//
//}
LKDBHelper增
LKDBModel *model=[[LKDBModel alloc]init];
model.model_id=self.count;
model.name=[NSString stringWithFormat:@"name-%ld",self.count];
if (self.count%2==0) {
model.isShow=YES;
}
else{
model.isShow=NO;
}
NSMutableArray *array=[[NSMutableArray alloc]init];
for (NSInteger i=0; i<self.count; i++) {
[array addObject:[NSNumber numberWithInteger:i]];
}
model.dataArray=array;
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
for (NSInteger i=0; i<self.count; i++) {
[dict setObject:[NSString stringWithFormat:@"dict-%ld",self.count] forKey:[NSNumber numberWithInteger:i]];
}
model.dataDictString=[NSString stringWithFormat:@"%@",dict];
[model saveToDB]
LKDBHelper刪
方式一(直接刪除某個model):
BOOL success=[self.helper deleteToDB:model];
方式二(根據(jù)條件刪除):
BOOL success=[self.helper deleteWithClass:[LKDBModel class] where:@{@"model_id":[NSNumber numberWithInteger:model.model_id]}];
LKDBHelper改
model.name=[NSString stringWithFormat:@"changeName-%ld",self.count];
BOOL success=[model saveToDB];
LKDBHelper查
[self.helper search:[LKDBModel class] where:nil orderBy:nil offset:0 count:0 callback:^(NSMutableArray * _Nullable array) {
NSLog(@"searchButtonClick:%@",array);
}];
coreData的使用
MagicalRecord初始化,一般在AppDelegate.m的didFinishLaunchingWithOptions函數(shù)進(jìn)行初始化
[MagicalRecord setupCoreDataStack];
[MagicalRecord setupCoreDataStackWithStoreNamed:@"MagicalRecord.sqlite"];
self.localContext = [NSManagedObjectContext MR_context];
MagicalRecord增
//初始化一個Model
MRModel *model=[MRModel MR_createEntityInContext:self.localContext];
//對該Model進(jìn)行賦值
model.id=self.count;
model.name=[NSString stringWithFormat:@"name-%ld",self.count];
if (self.count%2==0) {
model.isShow=YES;
}
else{
model.isShow=NO;
}
//這句話很重要。增刪改查都要用這個方法來保存數(shù)據(jù)
[self.localContext MR_saveToPersistentStoreAndWait];
MagicalRecord刪
- 先查再刪 刪除所有符合條件的數(shù)據(jù)
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"id=3"];
NSFetchRequest *request=[[NSFetchRequest alloc]init];
[request setPredicate:predicate];
NSEntityDescription *entity=[NSEntityDescription entityForName:@"MRModel" inManagedObjectContext:self.localContext];
[request setEntity:entity];
NSArray *arr=[self.localContext executeFetchRequest:request error:nil];
[arr enumerateObjectsUsingBlock:^(MRModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
BOOL success=[obj MR_deleteEntity];
if (success) {
NSLog(@"刪除成功");
}
else{
NSLog(@"刪除失敗");
}
}];
//必須要加上這句話,不然沒法刪除成功
[self.localContext MR_saveToPersistentStoreAndWait];
- 直接刪 刪除第一條符合條件的數(shù)據(jù)
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"id=1"];
BOOL success = [MRModel MR_deleteAllMatchingPredicate:predicate inContext:self.localContext];
if (success) {
NSLog(@"刪除成功");
}
else{
NSLog(@"刪除失敗");
}
[self.localContext MR_saveToPersistentStoreAndWait];
MagicalRecord改
- 用
MR_findByAttribute查找并修改
NSArray *array=[MRModel MR_findByAttribute:@"id" withValue:[NSNumber numberWithInteger:1]];
[array enumerateObjectsUsingBlock:^(MRModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.name=@"修改-name";
}];
[self.localContext MR_saveToPersistentStoreAndWait];
- 用
NSFetchRequest查找并修改
NSFetchRequest *request=[[NSFetchRequest alloc]init];
[request setPredicate:predicate];
NSEntityDescription *entity=[NSEntityDescription entityForName:@"MRModel" inManagedObjectContext:self.localContext];
[request setEntity:entity];
NSArray *arr=[self.localContext executeFetchRequest:request error:nil];
[arr enumerateObjectsUsingBlock:^(MRModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"id: %lld , \nname: %@ ,\nisShow:%d", obj.id, obj.name,obj.isShow);
obj.name=@"修改-name";
}];
[self.localContext MR_saveToPersistentStoreAndWait];
MagicalRecord查
- 查找所有
NSArray *array=[MRModel MR_findAll];
[array enumerateObjectsUsingBlock:^(MRModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"id: %lld , \nname: %@ ,\nisShow:%d", obj.id, obj.name,obj.isShow);
}];
- 根據(jù)條件查找
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"isShow=1"];
NSFetchRequest *request=[[NSFetchRequest alloc]init];
[request setPredicate:predicate];
NSEntityDescription *entity=[NSEntityDescription entityForName:@"MRModel" inManagedObjectContext:self.localContext];
[request setEntity:entity];
NSArray *arr=[self.localContext executeFetchRequest:request error:nil];
[arr enumerateObjectsUsingBlock:^(MRModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"id: %lld , \nname: %@ ,\nisShow:%d", obj.id, obj.name,obj.isShow);
}];
源碼:github