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,只是知足了本身的需求。