MagicalRecord学习笔记

前端之家收集整理的这篇文章主要介绍了MagicalRecord学习笔记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

以前都是用FMDB的,经过封装,FMDB的使用还是很方便的,多种方法增删改查都可以一行代码实现,并且不需要拼写sql语句,最近测试下FMDB的效率,还是比较慢的,删改查效率和coredata效率差不多少,但是添加数据效率慢的不是一点点,测试插入一个简单的Model类10w次,CoreData用时3~4s,FMDB用时大于66s! 慢了十几倍! 所以,个人认为,如果不需要增删改查大量数据,还是用fmdb比较方便,但是如果需要同时添加上万条数据,则使用CoreData来存储数据,如果数据量比较小,还是觉得使用FMDB的封装更简单一点.

作为一个程序开发者,应该的态度是接受变化,拥抱变化,不是吗?

首先是MagicRecord的配置

直接使用CoreData需要在AppDelegate中配置相关的代码,所以,使用MagicRecord时候也需要做相同的配置.

//配置CoreData,MagicRecord提供了以下几种方法. 意义顾名思义,不解释!

+ (void) setupCoreDataStack;
+ (void) setupCoreDataStackWithInMemoryStore;
+ (void) setupAutoMigratingCoreDataStack;

+ (void) setupCoreDataStackWithStoreNamed:(MR_nonnull NSString *)storeName;
+ (void) setupCoreDataStackWithAutoMigratingsqliteStoreNamed:(MR_nonnull NSString *)storeName;

+ (void) setupCoreDataStackWithStoreAtURL:(MR_nonnull NSURL *)storeURL;
+ (void) setupCoreDataStackWithAutoMigratingsqliteStoreAtURL:(MR_nonnull NSURL *)storeURL;

例如:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [MagicalRecord setupCoreDataStackWithAutoMigratingsqliteStoreNamed:@"MyDatabase.splite"];
    
    return YES;
}

其次需要在程序终止代码中写上如下代码:
// 程序将要终止时候执行的代码
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];

// 程序终止的时候清理CoreData相关配置.
    [MagicalRecord cleanUp];
}

所有关于数据存储的操作,不外乎增删改查! 本文例子将按照增删改查的分类来编写.

增添数据:

//添加记录方法就是,先创建一个对应的实体,赋值之后,将实体保存起来.
Person *person = [Person MR_createEntity];
person.firstName = @"韦";
person.lastName = @"东方";
person.age = 20;
//添加修改属性之后,将实体的状态保存下来
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

修改数据:

//修改数据的思路是这样的,首先还是查询,先查出来要修改的数据,然后修改相关的属性,最后保存当前的状态即可! 主要还是查询数据,按照一定的条件查询出来想要的数据,然后按照自己的需要操作数据.

Person *person = [Person MR_findFirstByAttribute:@"age" withValue:@20];
    if (person) {
        person.age = 21;
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    }

删除数据:

// 删除数据的顺序是,先查找出要删除的数据,然后执行下面方法删除实体,最后将删除后的状态保存下来.
- (BOOL) MR_deleteEntity;
- (BOOL) MR_deleteEntityInContext:(MR_nonnull NSManagedObjectContext *)context;

+ (BOOL) MR_deleteAllMatchingPredicate:(MR_nonnull NSPredicate *)predicate;
+ (BOOL) MR_deleteAllMatchingPredicate:(MR_nonnull NSPredicate *)predicate inContext:(MR_nonnull NSManagedObjectContext *)context;
//保存删除后的状态
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

查询数据:

//查询的话,方法太多了,整个分类都是`NSManagedObject (MagicalFinders)`,共有几十种方法可以用来查询,满足不同的查询效果,包含排序,可以使用谓词(NSPredicate)来封装. 

部分方法如下:

+ (MR_nullable MR_NSArrayOfNSManagedObjects) MR_findAll;
+ (MR_nullable MR_NSArrayOfNSManagedObjects) MR_findAllInContext:(MR_nonnull NSManagedObjectContext *)context;
+ (MR_nullable MR_NSArrayOfNSManagedObjects) MR_findAllSortedBy:(MR_nonnull NSString *)sortTerm ascending:(BOOL)ascending;
+ (MR_nullable MR_NSArrayOfNSManagedObjects) MR_findAllSortedBy:(MR_nonnull NSString *)sortTerm ascending:(BOOL)ascending inContext:(MR_nonnull NSManagedObjectContext *)context;
+ (MR_nullable MR_NSArrayOfNSManagedObjects) MR_findAllSortedBy:(MR_nonnull NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(MR_nullable NSPredicate *)searchTerm;
+ (MR_nullable MR_NSArrayOfNSManagedObjects) MR_findAllSortedBy:(MR_nonnull NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(MR_nullable NSPredicate *)searchTerm inContext:(MR_nonnull NSManagedObjectContext *)context;

+ (MR_nullable MR_NSArrayOfNSManagedObjects) MR_findAllWithPredicate:(MR_nullable NSPredicate *)searchTerm;
+ (MR_nullable MR_NSArrayOfNSManagedObjects) MR_findAllWithPredicate:(MR_nullable NSPredicate *)searchTerm inContext:(MR_nonnull NSManagedObjectContext *)context;

+ (MR_nullable instancetype) MR_findFirst;
+ (MR_nullable instancetype) MR_findFirstInContext:(MR_nonnull NSManagedObjectContext *)context;
+ (MR_nullable instancetype) MR_findFirstWithPredicate:(MR_nullable NSPredicate *)searchTerm;
+ (MR_nullable instancetype) MR_findFirstWithPredicate:(MR_nullable NSPredicate *)searchTerm inContext:(MR_nonnull NSManagedObjectContext *)context;
+ (MR_nullable instancetype) MR_findFirstWithPredicate:(MR_nullable NSPredicate *)searchterm sortedBy:(MR_nullable NSString *)property ascending:(BOOL)ascending;

猜你在找的Sqlite相关文章