Realm是和SQLite一樣用於數據存儲,但是它有幾個特點比其它的數據庫要好用:
1.跨平台 :現在絕大多數的應用開發並不僅僅只在 iOS 平台上進行開發,還要兼顧到 Android 平台的開發。為兩個平台設計不同的數據庫是愚蠢的,而使用 Realm 數據庫, iOS 和 Android 無需考慮內部數據的架構,調用 Realm 提供的 API 就可以完成數據的交換,實現 “ 一個數據庫,兩個平台無縫銜接 ” 。
2.簡單易用 : Core Data 和 SQLite 冗余、繁雜的知識和代碼足以嚇退絕大多數剛入門的開發者,而換用 Realm ,則可以極大地減少學習代價和學習時間,讓應用及早用上數據存儲功能。
3.可視化 : Realm 還提供了一個輕量級的數據庫查看工具,借助這個工具,開發者可以查看數據庫當中的內容,執行簡單的插入和刪除數據的操作。畢竟,很多時候,開發者使用數據庫的理由是因為要提供一些所謂的 “ 知識庫 ” 。
下面來了解一下Realm的一些主要的類的功能:
1.RLMRealm : RLMRealm 是框架的核心所在,是我們構建數據庫的訪問點,就如同 Core Data 的管理對象上下文( managed object context )一樣。出於簡單起見, realm 提供了一個名為 defaultRealm 的單例,在本教程中我們就僅使用這個單例來完成我們所需的功能。當然,我們也可以導入外部已經編寫好的 realm 數據庫文件,也可以在我們不需要將數據保存在硬盤上時使用 “ 內存實例對象 ” ( in-memory realm instance ),此外,還可以同時使用多個數據庫文件。
2.RLMObject :這是我們自定義的 realm 數據模型。創建數據模型的行為將會影響到數據庫的結構。要創建一個數據模型,我們只需要繼承 RLMObject ,然后設計我們想要存儲的屬性即可。
3.關系 (Relationships) :通過簡單地在數據模型中聲明一個 RLMObject 類型的屬性,我們就可以創建一個 “ 一對多 ” 的對象關系。同樣地,借助 RLMArray 我們還可以創建 “ 多對一 ” 和 “ 多對多 ” 的關系。
4.寫操作事務 (Write Transactions) :數據庫中的所有操作,比如創建、編輯,或者刪除對象,都必須在事務中完成。 “ 事務 ” 是指位於 beginWriteTransaction() 以及 commitWriteTransaction() 操作之間的代碼段。
5.查詢 (Queries) :要在數據庫中檢索信息,我們需要用到 “ 檢索 ” 操作。檢索最簡單的形式是對 RLMObject 對象發送 allObjects() 消息。如果需要檢索更復雜的數據,那么還可以使用斷言( predicates )、復合查詢以及結果排序等等操作。
6.RLMResults :這個類是執行任何查詢請求后所返回的類,其中包含了一系列的 RLMObjects 對象。和 NSArray 類似,我們可以用下標語法來對其進行訪問,並且還可以決定它們之間的關系。不僅如此,它還擁有許多更強大的功能,包括排序、查找等等操作。
好了,接下來直接看下如何使用簡單實用的Realm:
1.創建數據模型:
.h文件 創建你需要保存的內容對應的屬性
#import <Realm/Realm.h> @interface Person : RLMObject @property NSString *name; @property NSString *sex; @property int age; @end // This protocol enables typed collections. i.e.: // RLMArray<Person> RLM_ARRAY_TYPE(Person)
.m文件 沒特別需求不需要操作
#import "Person.h" @implementation Person // Specify default values for properties //+ (NSDictionary *)defaultPropertyValues //{ // return @{}; //} // Specify properties to ignore (Realm won't persist these) //+ (NSArray *)ignoredProperties //{ // return @[]; //} @end
controller的.m文件
#import "ViewController.h" #import "Person.h" #import "Realm.framework/Headers/Realm.h" @interface ViewController () { RLMRealm *_customRealm; } @property (weak, nonatomic) IBOutlet UITextField *name; @property (weak, nonatomic) IBOutlet UITextField *sex; @property (weak, nonatomic) IBOutlet UITextField *age; @property (nonatomic, strong) RLMResults *locArray; @property (nonatomic, strong) RLMNotificationToken *token; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. /* 可以使用默認的 _customRealm = [RLMRealm defaultRealm]; */ //自己創建一個新的RLMRealm NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *pathStr = paths.firstObject; _customRealm = [RLMRealm realmWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",pathStr,@"custom.realm"]]]; } - (IBAction)clickAdd:(id)sender { Person *person = [[Person alloc] init]; person.name = self.name.text; person.sex = self.sex.text; person.age = [self.age.text intValue]; // RLMRealm *realm = [RLMRealm defaultRealm]; //數據持久化 [_customRealm transactionWithBlock:^{ [_customRealm addObject:person]; }]; //也可以這樣 /* // 通過事務將數據添加到 Realm 中 [_customRealm beginWriteTransaction]; [_customRealm addObject:person]; [_customRealm commitWriteTransaction]; */ } - (IBAction)clickLog:(id)sender { /* self.locArray = [Person allObjectsInRealm:_customRealm]; */ /** * 根據年齡排序 */ self.locArray = [[Person allObjectsInRealm:_customRealm] sortedResultsUsingProperty:@"age" ascending:YES]; NSLog(@"%@",self.locArray); /** * 查找年齡大於18的 */ self.locArray = [[Person allObjectsInRealm:_customRealm] objectsWhere:@"age > 18"]; NSLog(@"%@",self.locArray); } - (IBAction)clickButton:(id)sender { for (Person *person in self.locArray) { NSLog(@"name = %@ age = %d sex = %@",person.name,person.age,person.sex); } Person *person = self.locArray.firstObject; //修改時 即 在一個事務中更新對象 [_customRealm beginWriteTransaction]; person.name = @"老王"; [_customRealm commitWriteTransaction]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
這就是Realm簡單的增刪改查,如需深入了解可以自己查資料,這里沒有寫更多的demo,只是滿足了自己的需求。
