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,只是满足了自己的需求。