Realm的简单使用

Realm个人理解: 它主要是一套移动端数据库框架,将对数据库表的操作转换为对对象的操作,如果是学过Java ORM框架的同学使用Realm起来应该上手比较快一点。而且据我所知Realm不仅支持Objective-C、Swift还支持Java等等。更多介绍请戳进来

PS:如何安装就不在这里多费口舌了,想必学习到这个框架的时候,各位大多应该已经不是新手了。接下来我们就直接来接触一下基本的增删改查操作。

一、这里是一个我们操作对象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

二、创建一个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是为了方便我们查看数据库文件。

2,在插入数据后我们还需要另外一个工具来查看数据库文件。

3,去到之前print的目录下找到default.realm文件并用上一步我们下载的工具Realm Browser查看,如下图:

Last: 关注我,谢谢。持续更新!

原文地址:https://www.cnblogs.com/1016882435AIDA/p/7025982.html