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 ),此外,還能夠同時使用多個數據庫文件。atom
2.RLMObject :這是咱們自定義的 realm 數據模型。建立數據模型的行爲將會影響到數據庫的結構。要建立一個數據模型,咱們只須要繼承 RLMObject ,而後設計咱們想要存儲的屬性便可。spa
3.關係 (Relationships) :經過簡單地在數據模型中聲明一個 RLMObject 類型的屬性,咱們就能夠建立一個 「 一對多 」 的對象關係。一樣地,藉助 RLMArray 咱們還能夠建立 「 多對一 」 和 「 多對多 」 的關係。設計
4.寫操做事務 (Write Transactions) :數據庫中的全部操做,好比建立、編輯,或者刪除對象,都必須在事務中完成。 「 事務 」 是指位於 beginWriteTransaction() 以及 commitWriteTransaction() 操做之間的代碼段。code
5.查詢 (Queries) :要在數據庫中檢索信息,咱們須要用到 「 檢索 」 操做。檢索最簡單的形式是對 RLMObject 對象發送 allObjects() 消息。若是須要檢索更復雜的數據,那麼還可使用斷言( predicates )、複合查詢以及結果排序等等操做。orm
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,只是知足了本身的需求。