基于iOS Realm数据库的使用实例详解

时间:2022-09-13 13:18:13

首先下载Realm源代码,https://realm.io/cn/docs/objc/latest

将下载的文件解压,从 ios/static/ 目录中将 Realm.framework 拖曳到 Xcode 工程的文件导航器内,然后在 Xcode 文件导航器中选中工程。然后选择应用目标,前往 Build Phases 选项卡。在 Link Binary with Libraries 部分中单击 + 按钮,然后添加 libc++.tbd 和 libz.tbd。这样还没有完,我们还需要安装插件,打开解压文件夹下的plugin,运行RealmPlugin.xcodeproj,然后重启XCode。

这样环境配置就完了,现在说重点如何去使用。

1.创建实体类

new File翻到最下面点击创建Realm实体类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#import <Realm/Realm.h>
@interface TestBean : RLMObject
@property NSString* name;
@property NSInteger age;
 
@end
// This protocol enables typed collections. i.e.:
// RLMArray<TestBean *><TestBean>
RLM_ARRAY_TYPE(TestBean)
#import "TestBean.h"
@implementation TestBean
 
@end

2.增删改查

这个增加,可以看到直接添加实体类就行了,这个RLMRealm就是数据库的操作类,以后所有操作都靠他。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-(void)addAction{
 NSLog(@"add");
 self.name = _tf_name.text;
 self.age = [_tf_age.text integerValue];
 
 TestBean* bean = [[TestBean alloc]init];
 bean.name = self.name;
 bean.age = self.age;
 RLMRealm *realm = [RLMRealm defaultRealm];
 [realm transactionWithBlock:^{
  [realm addObject:bean];
 }];
 
}

删除也是通过删除实体类来实现,但是我们需要首先通过查询出实体类才行。这个查询我们可以看到通过我们创建的实体类就能实现

?
1
2
3
4
5
6
7
8
9
10
11
-(void)deleteAction{
 
 NSLog(@"delete");
 RLMResults *dogs = [TestBean allObjects];
 RLMRealm *realm = [RLMRealm defaultRealm];
 [realm transactionWithBlock:^{
  for (TestBean *bean in dogs) {
   [realm deleteObject:bean];
  }
 }];
}

更新也是通过改变实体类的数据来实现

?
1
2
3
4
5
6
7
8
9
-(void)updateAction{
 NSLog(@"update");
 RLMResults *dogs = [TestBean allObjects];
 [[RLMRealm defaultRealm] transactionWithBlock:^{
  for (TestBean *bean in dogs) {
   bean.name = @"李四";
  }
 }];
}

查询我们还可以添加条件

?
1
2
3
4
5
6
// 使用断言字符串来查询
RLMResults<Dog *> *tanDogs = [Dog objectsWhere:@"color = 'tan' AND name BEGINSWITH 'B'"];
// 使用 NSPredicate 来查询
NSPredicate *pred = [NSPredicate predicateWithFormat:@"color = %@ AND name BEGINSWITH %@",
              @"tan", @"B"];
tanDogs = [Dog objectsWithPredicate:pred];

3.排序

这个是在查询完后去实现,通过实体类的key来排序。

?
1
2
3
4
5
6
7
8
9
10
-(void)sortAction{
 NSLog(@"query");
 RLMResults *dogs = [[TestBean allObjects] sortedResultsUsingKeyPath:@"name" ascending:YES];
 [[RLMRealm defaultRealm] transactionWithBlock:^{
  for (TestBean *bean in dogs) {
   NSLog(@"%@",bean.name);
  }
 }];
 
}

4.小结

只说了常用的功能,贴上官方中文文档供大家参考

https://realm.io/cn/docs/objc/latest

5.例子代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// ViewController.m
// BmobTest
//
// Created by shanreal-iOS on 2017/12/9.
// Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//
#import "ViewController.h"
#import "TestBean.h"
@interface ViewController ()
@property(nonatomic,strong)UIButton* add;
@property(nonatomic,strong)UIButton* delete;
@property(nonatomic,strong)UIButton* update;
@property(nonatomic,strong)UIButton* query;
@property(nonatomic,strong)UIButton* sort;
 
@property(nonatomic,strong)NSString* name;
@property(nonatomic,assign)NSInteger* age;
@property(nonatomic,strong)UITextField* tf_name;
@property(nonatomic,strong)UITextField* tf_age;
@end
@implementation ViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
 
 _add = [[UIButton alloc]initWithFrame:CGRectMake(10, 30, 100, 20)];
 [_add setTitle:@"add" forState:UIControlStateNormal];
 [_add setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [_add addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
 _delete = [[UIButton alloc]initWithFrame:CGRectMake(10, 60, 100, 20)];
 [_delete setTitle:@"delete" forState:UIControlStateNormal];
 [_delete setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [_delete addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
 _update = [[UIButton alloc]initWithFrame:CGRectMake(10, 90, 100, 20)];
 [_update setTitle:@"update" forState:UIControlStateNormal];
 [_update setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [_update addTarget:self action:@selector(updateAction) forControlEvents:UIControlEventTouchUpInside];
 
 _query = [[UIButton alloc]initWithFrame:CGRectMake(10, 120, 100, 20)];
 [_query setTitle:@"query" forState:UIControlStateNormal];
 [_query setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [_query addTarget:self action:@selector(queryAction) forControlEvents:UIControlEventTouchUpInside];
 _sort = [[UIButton alloc]initWithFrame:CGRectMake(10, 150, 100, 20)];
 [_sort setTitle:@"sort" forState:UIControlStateNormal];
 [_sort setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [_sort addTarget:self action:@selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
 
 _tf_name = [[UITextField alloc]initWithFrame:CGRectMake(50, 300, 200, 20)];
 _tf_name.placeholder=@"name";
 _tf_age = [[UITextField alloc]initWithFrame:CGRectMake(50, 350, 200, 20)];
 _tf_age.placeholder=@"age";
 [self.view addSubview:_add];
 [self.view addSubview:_delete];
 [self.view addSubview:_update];
 [self.view addSubview:_query];
 [self.view addSubview:_sort];
 [self.view addSubview:_tf_name];
 [self.view addSubview:_tf_age];
}
-(void)addAction{
 NSLog(@"add");
 self.name = _tf_name.text;
 self.age = [_tf_age.text integerValue];
 
 TestBean* bean = [[TestBean alloc]init];
 bean.name = self.name;
 bean.age = self.age;
 RLMRealm *realm = [RLMRealm defaultRealm];
 [realm transactionWithBlock:^{
  [realm addObject:bean];
 }];
 
}
-(void)deleteAction{
 
 NSLog(@"delete");
 RLMResults *dogs = [TestBean allObjects];
 RLMRealm *realm = [RLMRealm defaultRealm];
 [realm transactionWithBlock:^{
  for (TestBean *bean in dogs) {
   [realm deleteObject:bean];
  }
 }];
}
-(void)updateAction{
 NSLog(@"update");
 RLMResults *dogs = [TestBean allObjects];
 [[RLMRealm defaultRealm] transactionWithBlock:^{
  for (TestBean *bean in dogs) {
   bean.name = @"李四";
  }
 }];
}
-(void)queryAction{
 NSLog(@"query");
 RLMResults *dogs = [TestBean allObjects];
 [[RLMRealm defaultRealm] transactionWithBlock:^{
  for (TestBean *bean in dogs) {
   NSLog(@"%@",bean.name);
  }
 }];
 
}
-(void)sortAction{
 NSLog(@"query");
 RLMResults *dogs = [[TestBean allObjects] sortedResultsUsingKeyPath:@"name" ascending:YES];
 [[RLMRealm defaultRealm] transactionWithBlock:^{
  for (TestBean *bean in dogs) {
   NSLog(@"%@",bean.name);
  }
 }];
 
}
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}
 
@end

以上这篇基于iOS Realm数据库的使用实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/z979451341/article/details/78790419