Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 使用開源庫 MagicalRecord

使用開源庫 MagicalRecord

編輯:關於Android編程

MagicalRecordhttps://github.com/magicalpanda/MagicalRecord

注意: 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 的 中.被操作對象的接口應該包含增刪改查的方法,基本屬性不多不少的剛好與數據庫中的一條記錄相對應.

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.

實時映射記錄是一種操作數據庫的方式,一個數據庫的表被封裝成一個對象;這個對象中的一個實例會對應著該表中的一條記錄.當創建一個對象時,一條記錄也被插入到表中並保存起來.任何被加載的對象中的屬性信息都從數據庫中讀取;當一個對象更新時,這個數據庫表中對應的記錄也會更新.這個被封裝的類實現了實時操作的方法,且其屬性一一對應於數據庫中表的屬性.

-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 框架

\

2. 在 .pch 文件中引入頭文件 CoreData+MagicalRecord.h

\

注:只能在.pch文件中引頭文件,否則無法通過編譯

3. 創建 Model.xcdatamodeld 文件,並創建一個 Student 的 ENTITIES,最後創建出 Student 類

\

\

4. 在 Appdelete.m 文件中寫以下代碼

\

\

 

以下是增刪改查的基本操作,但注意一點,在做任何的數據庫操作之前,請先初始化以下,在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_ 前綴了

\

以下為代碼(提供block來通知存儲成功,異步操作)

\

以下為打印信息

----------------------------------------------------------------------------------------------------------------------------------------------------

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:
2014-03-13 11:17:43.617 StudyMagicalRecord[26416:60b] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0x2f4498) Created Main Queue Context:
2014-03-13 11:17:43.617 StudyMagicalRecord[26416:60b] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x2f4498) Set Default Context:
2014-03-13 11:17:43.618 StudyMagicalRecord[26416:60b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0xe74e040) → Saving 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 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: on *** BACKGROUND THREAD ***
2014-03-13 11:17:43.627 StudyMagicalRecord[26416:60b] YES

----------------------------------------------------------------------------------------------------------------------------------------------------

2.如何關閉MagicalRecord提供的打印信息?

修改MagicalRecord.h 23 行處的值,把 0 改為 1 即可.

\

 
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved