LKDBHelper和MagicalRecord的基本用法

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的使用

新建一個coreData


MagicalRecord初始化,一般在AppDelegate.mdidFinishLaunchingWithOptions函數(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容