realm介紹:
realm的學(xué)習(xí)筆記 http://www.itdecent.cn/p/95fd23c4ec5f
Realm是一個移動端的數(shù)據(jù)庫,Realm是SQLite和CoreData的替代者。它可以節(jié)省你成千上萬行代碼和數(shù)周的工作,并且讓你精巧的制作出令人驚嘆的用戶體驗。簡單易用,快速持久。
引用網(wǎng)站 :http://www.hangge.com/blog/cache/detail_891.html
準(zhǔn)備工作
- 1.安裝realm模版插件(使用模版創(chuàng)建對象模型):下載realm官網(wǎng)oc的demo(https://realm.io/docs/objc/latest/), 編譯plugin下的 RealmPlugin.xcodeproj項目,編譯成功 插件就安裝成功了,然后重啟就可以使用。
- 2.安裝成功之后創(chuàng)建項目,并手動導(dǎo)入框架或者使用cocopod導(dǎo)入(在剛才官網(wǎng)中有手動導(dǎo)入的步驟);
- 3.在蘋果商店下載一個Realm Browser 的軟件,查看realm數(shù)據(jù)庫文件;
- 準(zhǔn)備工作完成;
使用流程
- 1.導(dǎo)入頭文件#import < Realm/Realm.h >.
- 2.創(chuàng)建類,使用realm模版進(jìn)行創(chuàng)建(模版插件安裝成功就會看到realm模版),繼承于RLMObject.如圖:
@interface BLEBook : RLMObject
@property NSString *name;
@property NSInteger price;
@end
//設(shè)置主鍵
@implementation BLEPerson
+ (NSString *)primaryKey {
return @"id";
}
@end
- 3.在類中生成數(shù)據(jù)模型.
- 4.在需要的地方創(chuàng)建實例,使用Realm方法調(diào)用.
基本語法
插入
插入方式1:
//這種插入:有相同的主鍵會崩潰
//初始化book的三種方式
方式1:
BLEBook *book = [[BLEBook alloc]init];
book.name = @"C++語言";
book.price = 100;
方式2:
BLEBook *dicBook = [[BLEBook alloc]initWithValue:@{@"name":@"數(shù)據(jù)庫",@"price":@50}];
方式3:
BLEBook *arrBook = [[BLEBook alloc]initWithValue:@[@"C語言",@25]];
//在處理事務(wù)的時候做添加book
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addObject:book];
[realm addObject:dicBook];
[realm addObject:arrBook];
[realm commitWriteTransaction];
插入方式2:
這種插入:如果有相同主鍵會做修改 負(fù)責(zé)就添加
BLEPerson *author = [[BLEPerson alloc]init];
author.id = 101;
author.name = @"并er";
[[RLMRealm defaultRealm]beginWriteTransaction];
[BLEPerson createOrUpdateInRealm:[RLMRealm defaultRealm] withValue:author];
[[RLMRealm defaultRealm]commitWriteTransaction];
刪除
[[RLMRealm defaultRealm]beginWriteTransaction];
RLMResults *result = [BLEBook allObjects];
[[RLMRealm defaultRealm]deleteObject:result.lastObject];
[[RLMRealm defaultRealm]commitWriteTransaction];
修改
[[RLMRealm defaultRealm]beginWriteTransaction];
RLMResults *result = [BLEBook allObjects];
BLEBook *firstBook = [result objectAtIndex:0];
firstBook.price = 400;
BLEBook *book = result.lastObject;
book.price = 100;
[[RLMRealm defaultRealm]commitWriteTransaction];
查詢
//1.使用斷言字段查詢
RLMResults *result = [BLEBook objectsWhere:@"name BEGINSWITH 'C'"];
NSLog(@"%@",result);
//2.使用謂詞字段查詢
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"price == '50'"];
RLMResults *result = [BLEBook objectsWithPredicate:predicate];
NSLog(@"%@",result);
排序
RLMResults *sortBook = [[BLEBook allObjects]sortedResultsUsingProperty:@"name" ascending:NO];
NSLog(@"%@",sortBook);
添加通知
Realm有一個注冊通知的方法:addNotificationBlock:
作用:監(jiān)聽數(shù)據(jù)庫數(shù)據(jù)的改變,如果監(jiān)聽到數(shù)據(jù)庫數(shù)據(jù)改變,就會執(zhí)行通知回調(diào)。刷新界面更新界面數(shù)據(jù)。
實例:
這是我在A控制器引用了數(shù)據(jù)庫數(shù)據(jù),嘗試使用注冊通知監(jiān)聽數(shù)據(jù)的改變。
//必須強(qiáng)引用改通知(防止釋放)
@property (nonatomic, strong)RLMNotificationToken *token;
self.token = [[CHRealm defaultRealm]addNotificationBlock:^(NSString * _Nonnull notification, RLMRealm * _Nonnull realm) {
//監(jiān)聽到數(shù)據(jù)庫數(shù)據(jù)改變后,就會執(zhí)行此回調(diào)方法
[self.tableView reloadData];
}];
注意:在控制器釋放的時候要釋放監(jiān)聽(通知的基本用法)
- (void)dealloc {
[self.token stop];
}
此方法用起來簡直就是爽爆了,小伙伴們你怎么看 趕快試一下吧。
數(shù)據(jù)遷移
我們都知道在開發(fā)過程中,數(shù)據(jù)的模型有時會隨著開發(fā)的遞進(jìn),需要做適當(dāng)?shù)奶砑踊騽h除部分屬性,這就需要用到數(shù)據(jù)的遷移了。因為當(dāng)使用realm進(jìn)行了數(shù)據(jù)存儲后,必須經(jīng)過數(shù)據(jù)的遷移才能修改對應(yīng)的數(shù)據(jù)庫。實際上就是升級存儲在沙盒里的數(shù)據(jù)庫版本。具體代碼實現(xiàn)(didFinishLaunchingWithOptions中實現(xiàn))
//數(shù)據(jù)遷移
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// 設(shè)置新的架構(gòu)版本。這個版本號必須高于之前所用的版本號(如果您之前從未設(shè)置過架構(gòu)版本,那么這個版本號設(shè)置為 0)
config.schemaVersion = 2;
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
// 目前我們還未進(jìn)行數(shù)據(jù)遷移,因此 oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// 什么都不要做!Realm 會自行檢測新增和需要移除的屬性,然后自動更新硬盤上的數(shù)據(jù)庫架構(gòu)
CHLog(@"數(shù)據(jù)遷移");
}
};
[RLMRealmConfiguration setDefaultConfiguration:config];
[RLMRealm defaultRealm];
//每次修改了數(shù)據(jù)的模型的時候 就需改一次schemaVersion屬性(版本號 注意:版本號不能低于上一次的版本)
realm存儲路徑修改
realm默認(rèn)存儲在document文件中。
直接上代碼:
- (RLMRealm *)instanceRealm {
//默認(rèn)配置(此時存儲路徑指向document)
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
//修改路徑(刪除后兩個路徑path)
config.fileURL = [[config.fileURL URLByDeletingLastPathComponent]URLByDeletingLastPathComponent];
//修改路徑 (添加路徑path指向library的caches)
config.fileURL = [[[[config.fileURL URLByAppendingPathComponent:@"Library" isDirectory:YES] URLByAppendingPathComponent:@"Caches" isDirectory:YES] URLByAppendingPathComponent:@"chronos"] URLByAppendingPathExtension:@"realm"];
NSError *error = nil;
//創(chuàng)建realm
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:&error];
if (error) {
NSLog(@"%ld",error.code);
}
return realm;
}
運(yùn)行結(jié)束查看項目的document文件路徑 使用realm brower查看.realm 文件 實例:
注意:
具體的可以在官網(wǎng)查看。
因此設(shè)置可空的數(shù)字屬性必須是RLMInt、RLMFloat、RLMDouble或者RLMBool其中一個類型。