1.安装

Realm github下载地址

如果使用cocoapods,示例语句如下
platform :ios, '8.0'
target 'Simona_Realm' do
pod 'Realm', '~> 3.0.0-beta.3'
end

2.创建简易模型,继承于RLMObject,存储在数据库的模型都需要继承于RLMObject

  1. Person.h
  2. #import <Realm/Realm.h>
  3. RLM_ARRAY_TYPE(Person);
  4. @interface Person : RLMObject
  5. @property NSString *personId;
  6. @property BOOL status;
  7. @property NSString *sex;
  8. @end
  1. Person.m
  2. #import “Person.h”
  3. @implementation Person
  4. // 设置主键,确保数据唯一性
  5. + (NSString *)primaryKey {
  6. return @”personId”;
  7. }
  8. // 设置属性不为nil
  9. + (NSArray<NSString *> *)requiredProperties {
  10. return @[@”sex”];
  11. }
  12. // 设置忽略属性
  13. + (NSArray<NSString *> *)ignoredProperties {
  14. return @[];
  15. }
  16. // 设置默认值,对于不为null的属性,默认给空
  17. + (NSDictionary *)defaultPropertyValues {
  18. return @{@”sex”:@””};
  19. }
  20. //索引属性,主要用于搜索,根据性别进行搜索
  21. + (NSArray<NSString *> *)indexedProperties {
  22. return @[@”sex”,@”woman”];
  23. }
  24. @end

关于RLMObject
1.Realm忽略了OC的属性特性(如nonatomic, atomic, strong,retain, weak,copy等),所以在声明属性时可不写,,这些特性会一直生效直到被写入数据库。
2.Realm支持以下的类型BOOL, NSInteger, long, double, CGFloat, NSString, NSDate, NSData
3.定义了RLM_ARRAY_TYPE(Person)表示支持RLMArray属性,相当于允许RLMArray<Person>属性的使用,例如:在其他属性里可@property RLMArray<Person *><Person> *personal如此使用,相当于继承关系(本文暂不做赘述)

3.使用

<1.>存储

  1. 模拟存储一万条假数据
  2. RLMRealm *realm = [RLMRealm defaultRealm];
  3. [realm transactionWithBlock:^{
  4. for (int i = 0; i < 10000; i++) {
  5. Person *person = [[Person alloc]init];
  6. person.personId = [NSString stringWithFormat:@“张%d”,i];
  7. person.sex = @“Not scanned”;
  8. person.status = YES;
  9. [realm addObject:person];
  10. }
  11. }];

这里的
[realm transactionWithBlock:^{ }];
等同于
[realm beginWriteTransaction];
[realm commitWriteTransaction];
每次打开关闭数据库时都应执行此操作

<2.>删除

  1. RLMRealm *realm = [RLMRealm defaultRealm];
  2. [realm beginWriteTransaction];
  3. [realm deleteAllObjects];
  4. [realm commitWriteTransaction];

<3.>查询

  1. 1.根据谓词查询
  2. // RLMResults相当于查询到的数组
  3. NSPredicate *pred = [NSPredicate predicateWithFormat:@”status = %ld”, NO];
  4. RLMResults *result = [Person objectsWithPredicate:pred];
  5. Person *per = result[indexPath.row];
  6. 2.根据条件查询
  7. RLMResults *result = [Person objectsWhere:@”status = ‘NO’ AND personId BEGINSWITH ‘张1′”];
  8. Person *per = result[indexPath.row];
  9. 3.查询所有
  10. RLMResults *persons = [Person allObjects];
  11. Person *per = persons[indexPath.row];

<4.>更新

  1. 1.更新数据里某一属性值
  2. NSPredicate *pred = [NSPredicate predicateWithFormat:@“personId = %@”, strScan];
  3. RLMResults *result = [Person objectsWithPredicate:pred];
  4. if (result.count != 0) {
  5. RLMRealm *r = [RLMRealm defaultRealm];
  6. [r beginWriteTransaction];
  7. //直接修改
  8. Person *person = result[0];
  9. person.sex = @“Has been scanned”;
  10. person.status = NO;
  11. [r commitWriteTransaction];
  12. }
  13. 2.创建一个新值并更新数据库,createOrUpdateInRealm:通过主键来更新插入
  14. NSPredicate *pred = [NSPredicate predicateWithFormat:@“personId = %@”, strScan];
  15. RLMResults *result = [Person objectsWithPredicate:pred];
  16. if (result.count != 0) {
  17. Person *person = [[Person alloc]init];
  18. person.personId = @“Simona1”;
  19. person.sex = @“woman”;
  20. person.status = NO;
  21. RLMRealm *r = [RLMRealm defaultRealm];
  22. [r beginWriteTransaction];
  23. [Person createOrUpdateInRealm:r withValue:person];
  24. [r commitWriteTransaction];