MagicRecord For IOS 简介
一、概述
MagicalRecord 靈感來自于簡潔的Ruby語言中 Rails' Active Record 查詢方式.?MagicalRecord?這個開源庫的核心思想是:
- 1.清除 Core Data 相關(guān)的代碼
- 2.簡潔的清除,簡單的一行搜索記錄的功能
- 3.當(dāng)然允許使用NSFetchRequest,當(dāng)存在著復(fù)雜的搜索條件時
?二、使用
1. 導(dǎo)入框架
將 MagicalRecord 文件夾拖入到工程文件中,引入 CoreData.frame 框架
2. 引入頭文件
在 .pch 文件中引入頭文件 CoreData+MagicalRecord.h
注:只能在.pch文件中引頭文件,否則無法通過編譯
3. 創(chuàng)建 Model.xcdatamodeld 文件
? ?創(chuàng)建一個 Student 的 ENTITIES,最后創(chuàng)建出 Student 類
4. 在 Appdelete.m 文件中寫以下代碼
5.增刪改查:
以下是增刪改查的基本操作,但注意一點(diǎn),在做任何的數(shù)據(jù)庫操作之前,請先初始化以下,在Appdelete載入時初始化一次即可,否則找不到數(shù)據(jù)庫而崩潰。
(1)設(shè)置數(shù)據(jù)庫名字
??? [MagicalRecord setupCoreDataStackWithStoreNamed:@"Model.sqlite"];
(2)增加一條記錄
??? Student *person = [Student MR_createEntity];
??? person.name = @"Y.X.";
??? [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
注意:創(chuàng)建了對象后是需要執(zhí)行存儲操作的
(3)查詢
? ? 查詢所有的記錄
??? NSArray *students = [Student MR_findAll];
? ? 根據(jù)某個屬性某個條件查詢
??? NSArray *students = [Student MR_findByAttribute:@"name" withValue:@"Y.X."];
? ?根據(jù)排序取得搜索結(jié)果
??? NSArray *students = [Student MR_findAllSortedBy:@"name" ascending:YES];
? ?其它
? ?查詢所有記錄
? ?+ (NSArray *) MR_findAll;
? ?根據(jù)上下文句柄查詢所有記錄
? ?+ (NSArray *) MR_findAllInContext:(NSManagedObjectContext *)context;
? ?根據(jù)某個屬性排序查詢所有記錄
? ?+ (NSArray *) MR_findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending;
? ?根據(jù)某個屬性排序以及上下文操作句柄查詢所有記錄
? ?+ (NSArray *) MR_findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext?? ??*)context;
? ?根據(jù)某個屬性排序用謂詞來查詢記錄
? ?+ (NSArray *) MR_findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm;
? ?根據(jù)某個屬性排序以及上下文操作句柄用謂詞來查詢記錄
? ?+ (NSArray *) MR_findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context;
? ?根據(jù)謂詞查詢
? ?+ (NSArray *) MR_findAllWithPredicate:(NSPredicate *)searchTerm;
? ?根據(jù)謂詞以及上下文操作句柄來查詢
? ?+ (NSArray *) MR_findAllWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context;
? ?以下都是查詢一個對象時的操作,與上面重復(fù),不一一贅述
? ?+ (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;
(4)修改
??? NSArray *students = [Student MR_findByAttribute:@"name" withValue:@"Y.X."];
??? for (Student *tmp in students) {
??????? tmp.name = @"Jane";
??? }
??? [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
? ?注意:既然要修改首先得需要找到記錄,根據(jù)條件匹配找到記錄,然后修改,然后保存
(5)刪除
??? NSArray *students = [Student MR_findByAttribute:@"name" withValue:@"Frank"];
??? for (Student *tmp in students) {
??????? [tmp MR_deleteEntity];
??? }
??? [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];?
注意:既然要刪除首先得需要找到記錄,根據(jù)條件匹配找到記錄,然后刪除,然后保存
三、心得體會
如果項(xiàng)目中對于操作數(shù)據(jù)庫沒有性能要求請使用 CoreData 相關(guān)的開源庫吧.
CoreData 操作較為復(fù)雜, MagicalRecord 有著很多的特性,比如可以根據(jù)設(shè)置在主線程或者子線程中進(jìn)行操作,方便快捷,能入榜最佳10大開源庫自有其獨(dú)到的地方,會使用 MagicalRecord 需要具備一定的 CoreData 相關(guān)知識,本人也只是現(xiàn)學(xué)現(xiàn)用,但深知其可以為開發(fā)帶來的好處,使用數(shù)據(jù)庫的朋友有著如下的一些選擇.
1.?SQLite3???????? ? ????? C函數(shù)形式(本人之前做過干嵌入式開發(fā),即使是這樣也不推薦使用面向過程毫無對象概念的SQLite3,有更好的方式為什么不用呢?)
2.?FMDB????? ? ? ? ? ? ? ? 對SQLite3的封裝,有著對象的概念,熟悉SQ語句的朋友可以使用,但還沒有做到對象與記錄實(shí)時對應(yīng)
3.?CoreData?????? ?????? 他做到了對象與記錄實(shí)時對應(yīng)關(guān)系,使用其自身的搜索體系(不用SQ語言),但其基本的操作以及配置讓人望而卻步
4.?MagicalRecord? ??? 對 CoreData 官方的用法進(jìn)行了人性化的封裝,用過 CoreData 基本操作再使用 MagicalRecord 會深有體會
5.?ObjectiveRecord?? 也是對 CoreData 的人性化封裝,使用更加傻瓜,但傻瓜的代價就是犧牲了一些更強(qiáng)大的功能,在Github上搜索關(guān)鍵字即可?
四、附錄:
1.默認(rèn)的就是在后臺存儲的,不會阻塞主線程
我在 CoreData+MagicalRecord.h 文件中定義了宏 MR_SHORTHAND ,所以在方法中不需要 MR_ 前綴了
以下為代碼(提供block來通知存儲成功,異步操作)
2.如何關(guān)閉?MagicalRecord?提供的打印信息?
修改?MagicalRecord.h 23 行處的值,把 0 改為 1 即可.
總結(jié)
以上是生活随笔為你收集整理的MagicRecord For IOS 简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双层板在哪层覆铜_PCB覆铜箔层压板分类
- 下一篇: android 常用开发插件,Andro