美文网首页iOS开发
LKDBHelper和MagicalRecord的基本用法

LKDBHelper和MagicalRecord的基本用法

作者: 请叫我平爷 | 来源:发表于2018-09-03 15:38 被阅读116次

LKDBHelper是对FMDB的封装,可以在不写sql语句的情况下,使用model就可以全自动的进行数据表的创建,及数据的增、删、改、查。

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;

//由于发现字典类型不能直接存储,所以转成NSString进行存储。
@property (nonatomic , strong) NSString *dataDictString;

.m文件,首先引入#import "LKDBHelper.h"头文件,一般情况,设置一个主键名就行:

///**设置表名,一般不写,默认表名为类名*/
//+(NSString *)getTableName
//{
//    return @"LKDB.db";
//}

/**主键名*/
+(NSString *)getPrimaryKey
{
    return @"model_id";
}

/**复合主键*/
//+(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];

方式二(根据条件删除):

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函数进行初始化

    [MagicalRecord setupCoreDataStack];
    [MagicalRecord setupCoreDataStackWithStoreNamed:@"MagicalRecord.sqlite"];

    self.localContext    = [NSManagedObjectContext MR_context];

MagicalRecord

//初始化一个Model
    MRModel *model=[MRModel MR_createEntityInContext:self.localContext];
//对该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;
    }

//这句话很重要。增删改查都要用这个方法来保存数据
[self.localContext MR_saveToPersistentStoreAndWait];

MagicalRecord

  • 先查再删 删除所有符合条件的数据
    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];
  • 直接删 删除第一条符合条件的数据
    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);
    }];
  • 根据条件查找
    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

相关文章

  • LKDBHelper和MagicalRecord的基本用法

    LKDBHelper是对FMDB的封装,可以在不写sql语句的情况下,使用model就可以全自动的进行数据表的创建...

  • 定时器

    setTimeout和clearTimeout基本用法 setInterval和clearInterval基本用法...

  • CoreData及MagicalRecord学习

    本文是对 MagicalRecord github上的翻译 正文:注意: MagicalRecord 在 ARC...

  • 2019-11-16

    E战到底DAY14 SUMIF和SUMIFS函数 一.基本用法 SUMIF基本用法 SUMIFS基本用法 SUMI...

  • AFNetWorking

    AFNetworking的基本使用 网络请求 AFNetworking的基本用法和NSURLSession的用法基...

  • webSocket 和LKDBHelper的使用

    socketket与lkdbhelper来处理数据 客户需求: 当我们有需要从自己的后台推送消息给我们的用户时,用...

  • Sumif函数和Sumifs函数的用法

    今天我们来学习Sumif函数和Sumifs函数的用法,并进行比较。 一、基本用法 1.SUMIF的基本用法 (1)...

  • Android sqlite 的基本用法

    Android sqlite 的基本用法 1、sqlite 的基本语法和用法参考下面的博客 http://blog...

  • 2019-01-12

    不为人知的排序和筛选的高级用法 今日学习的内容主要学习了以下几个方面的内容: 1、基本用法:排序基本用法;筛选基本...

  • 2019-01-12

    今天学习到的就是排序和筛选的高级用法 1.基本用法, 基本用法的排序,也就是我们经常使用的筛选命令,基本上Exce...

网友评论

    本文标题:LKDBHelper和MagicalRecord的基本用法

    本文链接:https://www.haomeiwen.com/subject/rqnbwftx.html