数据持久化之Using CoreData with MagicalRecord

Posted llguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据持久化之Using CoreData with MagicalRecord相关的知识,希望对你有一定的参考价值。

         第五节里面,我介绍了CoreData的配置和主要的增删改查,可能非常多人会认为用它真繁琐.这里,我再介绍网上大神对它进行了人性化封装的第三方MagicalRecord,正如FMDB对sqlite进行了封装一样,MagicalRecord让你认为用CoreData非常方便.

      @基本配置:

         1.下载MagicalRecord,将里面的MagicalRecord目录拖入你的project

         2.确定你创建的project没有勾选"Use Core Data"

         3.导入CoreData.frame框架

         4.在.pch文件里引入头文件"CoreData+MagicalRecord.h"(仅仅能,必须在这里引用)


               @详细操作:

          1.初始化(在didFinishLaunchingWithOptions中)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    HMTRootViewController *rootVC = [[HMTRootViewController alloc] init];
    self.window.rootViewController = rootVC;
    
    //  初始化
    [MagicalRecord setupCoreDataStackWithStoreNamed:@"MT.sqlite"];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [MagicalRecord cleanUp];
}
                2.创建一个Model做測试,创建一个Person的Entity

技术分享

            3.增删改查详细操作

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //  初始化一个Person对象
    /**
     *  这里要注意:默认,xcdatamodeld实体描写叙述表名(name)和类名(class)必须保持一致
     *  假设name和class不一致,实现MagicalRecord_MOGenerator协议中得entityName方法来改变
     @implementation NSManagedObject (MagicalRecord)
     
     + (NSString *) MR_entityName;
     {
     NSString *entityName;
     
     if ([self respondsToSelector:@selector(entityName)])
     {
     entityName = [self performSelector:@selector(entityName)];
     }
     
     if ([entityName length] == 0) {
     entityName = NSStringFromClass(self);
     }
     
     return entityName;
     }
     */
    Person *person = [Person MR_createEntity];
    person.name = @"HMT";
    person.sex = @"男";
    person.age = @25;
    
}

//  加入操作
- (void)addDataOperation{

    // 文档解释:For any entities to actually be saved / updated / deleted on disk call following method(添加,更新,删除 都要用这种方法来保存数据)
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];;
}

//  查询操作
- (void)selectDataOperation{

    //  find数据库中全部的人
    NSArray *peoples = [Person MR_findAll];
    //  find数据库中第一条记录
    Person *firPerson = [Person MR_findFirst];
    //  find数据库中全部name属性为"HMT"的人并依照年龄age排序
    NSArray *otherPeoples = [Person MR_findByAttribute:@"name" withValue:@"HMT" andOrderBy:@"age" ascending:YES];

}

//  更新操作
- (void)upDataOperation{

    //  选定要改动的人
    NSArray *persons = [Person MR_findByAttribute:@"name" withValue:@"HMT"];
    for (Person *person in persons) {
        person.name = @"WDQ";
    }
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    
}

//  删除操作
- (void)deleteDataOperation{

    //  delete数据库中全部人
    [Person MR_truncateAll];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    
    //  依据条件delete特定的某个人
    NSArray *persons = [Person MR_findByAttribute:@"name" withValue:@"HMT"];
    for (Person *person in persons) {
        [person MR_deleteEntity];
    }
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

}
          4.额外配置(取消前缀名和不打印日志信息)

//If you want to omit the "MR_" prefix to all MagicalRecord realted method calls define following before importing the MagicalRecord header
#define MR_SHORTHAND

//Turn off MagicalRecord logging, again by defining following before header import
#define MR_ENABLE_ACTIVE_RECORD_LOGGING 0


               @以上仅仅是一些基本操作,其它能够自己去查看头文件








以上是关于数据持久化之Using CoreData with MagicalRecord的主要内容,如果未能解决你的问题,请参考以下文章

Swift之深入解析Core Data数据管理的集成指南

IOS开发之记录用户登陆状态

集成touchgfx 之 《Using C code with TouchGFX》

IOS开发之记录用户登陆状态,ios开发用户登陆

认识CoreData—初识CoreData

IOS数据持久化之归档NSKeyedArchiver