Realm的簡單使用

Realm我的理解: 它主要是一套移動端數據庫框架,將對數據庫表的操做轉換爲對對象的操做,若是是學過Java ORM框架的同窗使用Realm起來應該上手比較快一點。並且據我所知Realm不只支持Objective-C、Swift還支持Java等等。更多介紹請戳進來數據庫

PS:如何安裝就不在這裏多費口舌了,想必學習到這個框架的時候,各位大多應該已經不是新手了。接下來咱們就直接來接觸一下基本的增刪改查操做。app

1、這裏是一個咱們操做對象Person的.h文件,在引入Realm後咱們的對象必須繼承自RLMObject。框架

 1 #import <Realm/Realm.h>
 2 
 3 @interface Person : RLMObject
 4 
 5 @property NSInteger _ID;
 6 
 7 @property NSString *name;
 8 
 9 @property NSInteger age;
10 
11 @property NSString *sex;
12 
13 @end

2、建立一個Viewcontroller在裏面添加四個按鈕並添加相應事件工具

  1 #import "RealmViewController.h"
  2 
  3 @interface RealmViewController ()
  4 
  5 @property NSInteger IDNumber;
  6 
  7 @end
  8 
  9 @implementation RealmViewController
 10 
 11 - (void)viewDidLoad {
 12     [super viewDidLoad];
 13     
 14     _IDNumber = 1000;
 15     
 16     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 17     NSString *docDir = [paths objectAtIndex:0];
 18     NSLog(@"%@", docDir);
 19 
 20 }
 21 
 22 - (IBAction)insertAction:(id)sender {
 23     //這裏主要是幫咱們建立一個default.realm數據文件(數據庫名可更改,後期介紹)
 24     RLMRealm *realm = [RLMRealm defaultRealm];
 25     
 26     //打開數據庫事務
 27     [realm transactionWithBlock:^(){
 28         Person* _temp = [[Person alloc] init];
 29         _temp._ID = _IDNumber++;
 30         _temp.name = @"realm";
 31         _temp.age = 26;
 32         _temp.sex = @"male";
 33         //添加到數據庫
 34         [realm addObject:_temp];
 35         //提交事務
 36         [realm commitWriteTransaction];
 37     }];
 38 }
 39 
 40 - (IBAction)updateAction:(id)sender {
 41     //數據庫操做對象
 42     RLMRealm *realm = [RLMRealm defaultRealm];
 43     
 44     [realm transactionWithBlock:^(){
 45         //得到對象
 46         RLMResults* result = [Person allObjects];
 47         //得到第一個對象
 48         Person* temp = [result objectAtIndex:0];
 49         
 50         //修改sex
 51         temp.sex = @"ttt";
 52         
 53         //提交事務,即被修改
 54         [realm commitWriteTransaction];
 55         
 56     }];
 57 }
 58 
 59 - (IBAction)deleteAction:(id)sender {
 60     //數據庫操做對象
 61     RLMRealm *realm = [RLMRealm defaultRealm];
 62     
 63     [realm transactionWithBlock:^(){
 64         
 65     //得到對象
 66     RLMResults* result = [Person allObjects];
 67     //刪除第一個元素
 68     [realm deleteObject:result.firstObject];
 69 
 70     }];
 71 }
 72 
 73 
 74 - (IBAction)selectAction:(id)sender {
 75 
 76      //得到當前全部數據
 77     RLMResults* tempArray = [Person allObjects];
 78     
 79     for (Person* model in tempArray) {
 80         //打印數據
 81         NSLog(@"ID : %ld, name : %@, age : %ld , sex : %@",model._ID,model.name,model.age,model.sex);
 82         
 83     }
 84 }
 85 
 86 
 87 - (void)didReceiveMemoryWarning {
 88     [super didReceiveMemoryWarning];
 89     // Dispose of any resources that can be recreated.
 90 }
 91 
 92 /*
 93 #pragma mark - Navigation
 94 
 95 // In a storyboard-based application, you will often want to do a little preparation before navigation
 96 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 97     // Get the new view controller using [segue destinationViewController].
 98     // Pass the selected object to the new view controller.
 99 }
100 */
101 
102 @end

注意:在insert的對象中對象的全部屬性必須正確賦值,不然插入失敗。學習

解析:1,我在ViewDidLoad中將當前的Document目錄進行Print是爲了方便咱們查看數據庫文件。spa

2,在插入數據後咱們還須要另一個工具來查看數據庫文件。code

3,去到以前print的目錄下找到default.realm文件並用上一步咱們下載的工具Realm Browser查看,以下圖:對象

Last: 關注我,謝謝。持續更新!blog

相關文章
相關標籤/搜索