MagicalRecord Learning Notes

Keywords: SQL

FMDB used to be used in the past. After encapsulation, the use of FMDB is still very convenient. Many methods of adding, deleting and modifying can be implemented in one line of code without spelling sql statements. Recently, the efficiency of FMDB under test is relatively slow. The efficiency of deleting and modifying and coredata is almost low, but the efficiency of adding data is not a little slow. The test inserts a simple Model class 10 W times, CoreDat Dat. A takes 3 to 4 seconds, FMDB takes more than 66s! It's more than ten times slower! So, I think it's more convenient to use fmdb if you don't need to add, delete and change a lot of data, but if you need to add tens of thousands of data at the same time, you can use CoreData to store data. If the amount of data is small, you still feel that you need to use it. Packaging of FMDB It's simpler.

As a programmer, the attitude should be to accept change and embrace change, shouldn't it?

First, the MagicRecord configuration

Using CoreData directly requires configuring the relevant code in AppDelegate, so you need to do the same configuration when using MagicRecord.

//To configure CoreData, MagicRecord provides the following methods. The meaning, as the name implies, is not explained!

+ (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;

//For example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"MyDatabase.splite"];

    return YES;
}

//Next, you need to write the following code in the program termination code:
// Code to execute when the program is about to terminate
- (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];

// Clean up the CoreData configuration when the program terminates.
    [MagicalRecord cleanUp];
}

All the operations about data storage are just adding, deleting and modifying. The examples in this paper will be compiled according to the classification of adding, deleting and modifying.

Add data:

//The way to add records is to create a corresponding entity and save the entity after assignment.
Person *person = [Person MR_createEntity];
person.firstName = @"Wei";
person.lastName = @"east";
person.age = 20;
//After adding modified attributes, save the state of the entity
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

Modify data:

//The idea of modifying data is as follows: first, query, first find out the data to be modified, then modify the relevant attributes, and finally save the current status. The main idea is to query data, query the desired data according to certain conditions, and then operate the data according to their own needs.

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

Delete data:

// The order of deleting data is to find the data to be deleted first, then to delete the entity by the following method, and finally to save the deleted state.
- (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;
//Save deleted status
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

Query data:

// There are too many ways to query, and the whole classification is `NSManagedObject'. There are dozens of ways to query, which can satisfy different query effects, including sorting, and can be encapsulated with predicates. 

Some methods are as follows:

+ (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;

Posted by Paragon on Thu, 18 Apr 2019 02:54:34 -0700