使用开源库 MagicalRecord 操作 CoreData

sqlite 1

MagicalRecord  https://github.com/magicalpanda/MagicalRecord

sqlite 2

留神:  MagicalRecord 在 ARC 下运作,Core
Data 是 ORM
方案,听大人讲带来的难为比好处多,且 Core Data
建立的表没有主键,但对此对数据库没有品质要求,进行不难的数量操作完全够用,能简化无数的代码量.

MagicalRecord

In software engineering, the active record pattern is a design pattern
found in software that stores its data in relational databases. It was
named by Martin Fowler in his book Patterns of Enterprise Application
Architecture. The interface to such an object would include functions
such as Insert, Update, and Delete, plus properties that correspond
more-or-less directly to the columns in the underlying database table.

在软件工程中,对象与数据库中的记录实时映射是一种设计情势,在拍卖关系型数据库的的软件中多有现身.那种设计方式被记录在
Martin Fowler 的<Patterns of Enterprise Application Architecture>
中.被操作对象的接口应该包涵增删改查的点子,基本天性不多不少的刚巧与数据库中的一条记下相对应.

Active record is an approach to accessing data in a database. A
database table or view is wrapped into a class; thus an object
instance is tied to a single row in the table. After creation of an
object, a new row is added to the table upon save. Any object loaded
gets its information from the database; when an object is updated, the
corresponding row in the table is also updated. The wrapper class
implements accessor methods or properties for each column in the table
or view.

style=”color: #0000ff;”>实时映射记录是一种操作数据库的点子,一个数据库的表被封装成一个目标;那一个目标中的一个实例会对应着该表中的一条记录.当成立一个目标时,一条记下也被插入到表中并保存起来.任何被加载的靶子中的属性新闻都从数据库中读取;当一个目的更新时,这么些数据库表中对应的记录也会更新.那一个被打包的类完结了实时操作的方法,且其属性一一对应于数据库中表的属性.

– Wikipedia

MagicalRecord was inspired by the ease of Ruby on Rails’ Active Record
fetching. The goals of this code are:

  • Clean up my Core Data related code
  • Allow for clear, simple, one-line fetches
  • Still allow the modification of the NSFetchRequest when request
    optimizations are needed

MagicalRecord 灵感来自于不难的Ruby语言中
Rails’ Active Record 查询格局. MagicalRecord
那些开源库的核感情想是:

  • 免除 Core Data 相关的代码
  • 切中时弊的解除,不难的一条龙搜索记录的效果
  • 理所当然允许使用NSFetchRequest,当存在着错综复杂的物色条件时

 

恶性的翻译请勿见怪,以下是自身在最新的 Xcode
5.1 开 ARC 的条件下的应用教程.

 

  1. 将 MagicalRecord 文件夹拖入到工程文件中,引入 CoreData.frame 框架

sqlite 3

  1. 在 .pch 文件中引入头文件 CoreData+MagicalRecord.h

sqlite 4

注:只好在.pch文件中引头文件,否则无法透过编译

  1. 创建 Model.xcdatamodeld 文件,并创办一个 Student 的
    ENTITIES,最后成立出 Student 类

sqlite 5

sqlite 6

  1. 在 Appdelete.m 文件中写以下代码

sqlite 7

sqlite 8

 

以下是增删改查的基本操作,但只顾一点,在做其他的数据库操作此前,请先起首化以下,在Appdelete载入时初阶化一遍即可,否则找不到数据库而夭亡,你懂的.

    //设置数据库名字
    [MagicalRecord setupCoreDataStackWithStoreNamed:@”Model.sqlite”];

 

增加

日增一条记下
    Student *person = [Student MR_createEntity];
    person.name = @”Y.X.”;
    [[NSManagedObjectContext MR_defaultContext]
MR_saveToPersistentStoreAndWait];

瞩目:创制了对象后是内需实践存储操作的

查询

查询所有的笔录

    NSArray *students = [Student MR_findAll];

基于某个属性某个条件查询

    NSArray *students = [Student MR_findByAttribute:@”name”
withValue:@”Y.X.”];

 

依照排序取得搜索结果

    NSArray *students = [Student MR_findAllSortedBy:@”name”
ascending:YES];

 

自家不一一列举了,查看一下头文件就知晓了.

查询所有记录

+ (NSArray *) MR_findAll;

基于上下文句柄查询所有记录

  • (NSArray *) MR_findAllInContext:(NSManagedObjectContext *)context;

依照某个属性排序查询所有记录

  • (NSArray *) MR_findAllSortedBy:(NSString *)sortTerm
    ascending:(BOOL)ascending;

据悉某个属性排序以及上下文操作句柄查询所有记录

  • (NSArray *) MR_findAllSortedBy:(NSString *)sortTerm
    ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;

依据某个属性排序用谓词来查询记录

  • (NSArray *) MR_findAllSortedBy:(NSString *)sortTerm
    ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm;

根据某个属性排序以及上下文操作句柄用谓词来询问记录

  • (NSArray *) MR_findAllSortedBy:(NSString *)sortTerm
    ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm
    inContext:(NSManagedObjectContext *)context;

据悉谓词查询

  • (NSArray *) MR_findAllWithPredicate:(NSPredicate *)searchTerm;

依据谓词以及上下文操作句柄来询问

  • (NSArray *) MR_findAllWithPredicate:(NSPredicate *)searchTerm
    inContext:(NSManagedObjectContext *)context;

以下都以询问一个目的时的操作,与地方重复,不一一赘述

  • (instancetype) MR_findFirst;
  • (instancetype) MR_findFirstInContext:(NSManagedObjectContext
    *)context;
  • (instancetype) MR_findFirstWithPredicate:(NSPredicate
    *)searchTerm;
  • (instancetype) MR_findFirstWithPredicate:(NSPredicate *)searchTerm
    inContext:(NSManagedObjectContext *)context;
  • (instancetype) MR_findFirstWithPredicate:(NSPredicate *)searchterm
    sortedBy:(NSString *)property ascending:(BOOL)ascending;
  • (instancetype) MR_findFirstWithPredicate:(NSPredicate *)searchterm
    sortedBy:(NSString *)property ascending:(BOOL)ascending
    inContext:(NSManagedObjectContext *)context;
  • (instancetype) MR_findFirstWithPredicate:(NSPredicate *)searchTerm
    andRetrieveAttributes:(NSArray *)attributes;
  • (instancetype) MR_findFirstWithPredicate:(NSPredicate *)searchTerm
    andRetrieveAttributes:(NSArray *)attributes
    inContext:(NSManagedObjectContext *)context;
  • (instancetype) MR_findFirstWithPredicate:(NSPredicate *)searchTerm
    sortedBy:(NSString *)sortBy ascending:(BOOL)ascending
    andRetrieveAttributes:(id)attributes, …;
  • (instancetype) MR_findFirstWithPredicate:(NSPredicate *)searchTerm
    sortedBy:(NSString *)sortBy ascending:(BOOL)ascending
    inContext:(NSManagedObjectContext *)context
    andRetrieveAttributes:(id)attributes, …;
  • (instancetype) MR_findFirstByAttribute:(NSString *)attribute
    withValue:(id)searchValue;
  • (instancetype) MR_findFirstByAttribute:(NSString *)attribute
    withValue:(id)searchValue inContext:(NSManagedObjectContext
    *)context;
  • (instancetype) MR_findFirstOrderedByAttribute:(NSString *)attribute
    ascending:(BOOL)ascending;
  • (instancetype) MR_findFirstOrderedByAttribute:(NSString *)attribute
    ascending:(BOOL)ascending inContext:(NSManagedObjectContext
    *)context;

修改

    NSArray *students = [Student MR_findByAttribute:@”name”
withValue:@”Y.X.”];
    for (Student *tmp in students) {
        tmp.name = @”Jane”;
    }
    [[NSManagedObjectContext MR_defaultContext]
MR_saveToPersistentStoreAndWait];

只顾:既然要修改首先得需求找到记录,依照规则协作找到记录,然后修改,然后保留

 

删除

    NSArray *students = [Student MR_findByAttribute:@”name”
withValue:@”Frank”];
    for (Student *tmp in students) {
        [tmp MR_deleteEntity];
    }
    [[NSManagedObjectContext MR_defaultContext]
MR_saveToPersistentStoreAndWait]; 

小心:既然要删减首先得需求找到记录,依照标准相当找到记录,然后删除,然后保留

 

心得体会

假定项目中对此操作数据库没有品质须要请使用
CoreData 相关的开源库吧.

CoreData 操作比较复杂, MagicalRecord
有着许多的个性,比如可以依据设置在主线程可能子线程中开展操作,方便飞快,能入榜最佳10大开源库自有其别出心裁的地点,会动用
MagicalRecord 须要拥有一定的 CoreData
相关文化,本身也只是现学现用,但深知其可以为开发拉动的益处,使用数据库的情侣有所如下的一些拔取.

  1. SQLite3                
    C函数方式(本身以前做过干嵌入式开发,纵然是如此也不推荐使用面向进程不要对象概念的SQLite3,有更好的点子为何不用吧?)

  2. FMDB                   
    对SQLite3的包装,有着对象的概念,熟识SQ语句的爱人可以行使,但还尚无成功对象与记录实时对应

  3. CoreData             
    他达成了目的与记录实时对应提到,使用其自己的寻找连串(不用SQ语言),但其主干的操作以及配置令人恐惧

  4. MagicalRecord      对
    CoreData 官方的用法举办了人性化的卷入,用过 CoreData 基本操作再使用
    MagicalRecord 会深有体会

  5. ObjectiveRecord   也是对
    CoreData
    的人性化封装,使用进一步傻瓜,但傻瓜的代价就是捐躯了一部分更有力的功力,在Github上寻找关键字即可

 

 

附录:

1.暗中同意的就是在后台存储的,不会堵塞主线程

自我在 CoreData+MagicalRecord.h
文件中定义了宏 MR_SHORTHAND ,所以在措施中不要求 MR_ 前缀了

sqlite 9

以下为代码(提供block来打招呼存储成功,异步操作)

sqlite 10

以下为打印音信


2014-03-13
11:17:43.616 StudyMagicalRecord[26416:60b]
+[NSManagedObjectContext(MagicalRecord)
MR_contextWithStoreCoordinator:](0x2f4498) -> Created Context
UNNAMED
2014-03-13
11:17:43.616 StudyMagicalRecord[26416:60b]
+[NSManagedObjectContext(MagicalRecord)
MR_setRootSavingContext:](0x2f4498) Set Root Saving Context:
<NSManagedObjectContext: 0xe74d910>
2014-03-13
11:17:43.617 StudyMagicalRecord[26416:60b]
+[NSManagedObjectContext(MagicalRecord)
MR_newMainQueueContext](0x2f4498) Created Main Queue Context:
<NSManagedObjectContext: 0xe74e040>
2014-03-13
11:17:43.617 StudyMagicalRecord[26416:60b]
+[NSManagedObjectContext(MagicalRecord)
MR_setDefaultContext:](0x2f4498) Set Default Context:
<NSManagedObjectContext: 0xe74e040>
2014-03-13
11:17:43.618 StudyMagicalRecord[26416:60b]sqlite,
-[NSManagedObjectContext(MagicalSaves)
MR_saveWithOptions:completion:](0xe74e040) → Saving
<NSManagedObjectContext (0xe74e040): *** DEFAULT ***> on
*** MAIN THREAD ***
2014-03-13
11:17:43.618 StudyMagicalRecord[26416:60b]
-[NSManagedObjectContext(MagicalSaves)
MR_saveWithOptions:completion:](0xe74e040) → Save Parents? 1
2014-03-13
11:17:43.619 StudyMagicalRecord[26416:60b]
-[NSManagedObjectContext(MagicalSaves)
MR_saveWithOptions:completion:](0xe74e040) → Save Synchronously?
0
2014-03-13
11:17:43.619 StudyMagicalRecord[26416:60b] time

2014-03-13
11:17:43.622 StudyMagicalRecord[26416:60b]
-[NSManagedObjectContext(MagicalRecord)
MR_contextWillSave:](0xe74e040) Context DEFAULT is about to save.
Obtaining permanent IDs for new 1 inserted objects
2014-03-13
11:17:43.623 StudyMagicalRecord[26416:60b]
-[NSManagedObjectContext(MagicalSaves)
MR_saveWithOptions:completion:](0xe74d910) → Saving
<NSManagedObjectContext (0xe74d910): *** BACKGROUND SAVING (ROOT)
***> on *** MAIN THREAD ***
2014-03-13
11:17:43.624 StudyMagicalRecord[26416:60b]
-[NSManagedObjectContext(MagicalSaves)
MR_saveWithOptions:completion:](0xe74d910) → Save Parents? 1
2014-03-13
11:17:43.624 StudyMagicalRecord[26416:60b]
-[NSManagedObjectContext(MagicalSaves)
MR_saveWithOptions:completion:](0xe74d910) → Save Synchronously?
0
2014-03-13
11:17:43.625 StudyMagicalRecord[26416:1303]
-[NSManagedObjectContext(MagicalRecord)
MR_contextWillSave:](0xe74d910) Context BACKGROUND SAVING (ROOT) is
about to save. Obtaining permanent IDs for new 1 inserted
objects
2014-03-13
11:17:43.626 StudyMagicalRecord[26416:1303]
__70-[NSManagedObjectContext(MagicalSaves)
MR_saveWithOptions:completion:]_block_invoke25(0xe74d910) → Finished
saving: <NSManagedObjectContext (0xe74d910): *** BACKGROUND SAVING
(ROOT) ***> on *** BACKGROUND THREAD ***
2014-03-13
11:17:43.627 StudyMagicalRecord[26416:60b] YES


2.如何关闭 MagicalRecord
提供的打印信息?

修改 MagicalRecord.h 23 行处的值,把
0 改为 1 即可.

sqlite 11

网站地图xml地图