Realm数据库的使用(一)数据库的简单介绍和模型的创建

Realm 是一种可以替代SQLite 和CoreData的移动端数据库

使用前提:

  • iOS >= 7 or Mac OS X >= 10.9
  • Xcode >= 6
  • Both Objective-C & Swift are supported.

安装:

可通过CocoaPods加入项目中

pod serarch 'Realm'

Realm浏览器/数据库管理器

可以通过工具查看和浏览数据

 

Xcode 插件:

一个可以快速创建Realm模型的插件

模型:

Realm 数据模型其实就是传统的OC的类,只不过这里需要继承自RLMObject而不是NSObject

一个简单的Person类模型

 1 #import <Realm/Realm.h>
 2 
 3 @interface Person : RLMObject
 4 @property NSString *name;
 5 @property int age;
 6 @property NSDate *birthdate;
 7 @end
 8 
 9 // This protocol enables typed collections. i.e.:
10 // RLMArray<Person>
11 RLM_ARRAY_TYPE(Person)

属性(property)种类

Realm支持以下的属性(property)种类: BOOL, bool, int, NSInteger, long, float, double, CGFloat, NSString, NSDate 和 NSData。

也可以使用RLMArray\<_Object_\> 和 RLMObject来模拟对一或对多的关系——Realm也支持RLMObject继承。

属性(property)特性(attributes)

请注意Realm忽略了objective-c的property attributes, 像 nonatomic, atomic, strong, copy, weak 等等。 所以,在写入模型的时候不要使用任何的property attributes。但是,假如你设置了,这些attributes会一直生效直到RLMObject被写入realm数据库。 无论RLMObject在或不在realm中,为getter和setter自定义的名字都能正常工作

数据模型定制

  • +attributesForProperty: 可以被重写来来提供特定属性(property)的属性值(attrbutes)例如某个属性值要添加索引。
  • @interface Book : RLMObject
    @property float price;
    @property NSString *title;
    @end
    
    @implementation Book
    + (NSArray *)indexedProperties {
      return @[@"title"];
    }
    @end

     

  • +defaultPropertyValues 可以被重写,用以为新建的对象提供默认值。
  • @interface Book : RLMObject
    @property float price;
    @property NSString *title;
    @end
    
    @implementation Book
    + (NSDictionary *)defaultPropertyValues {
        return @{@"price" : @0, @"title": @""};
    }
    @end

     

  • +primaryKey 可以被重写来设置模型的主键。定义主键可以提高效率并且确保唯一性。
  • @interface Person : RLMObject
    @property NSInteger id;
    @property NSString *name;
    @end
    
    @implementation Person
    + (NSString *)primaryKey {
        return @"id";
    }
    @end

     

  • ignoredProperties 可以被重写来防止Realm存储模型属性。
  • @interface Person : RLMObject
    @property NSInteger tmpID;
    @property (readonly) NSString *name; // read-only properties are automatically ignored
    @property NSString *firstName;
    @property NSString *lastName;
    @end
    
    @implementation Person
    + (NSArray *)ignoredProperties {
        return @[@"tmpID"];
    }
    - (NSString *)name {
        return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
    }
    @end

    存储对象

  • Rrealm的对象可以被实例化并且被单独使用,和其他常规对象无异。 如果你想要在多个线程中共享或者永久保存以重复使用对象,你必须将其存储到Realm数据库中——这个操作必须在写事务中完成。 你可以参照如下代码添加一个对象:
  • // Create object
    Person *author = [[Person alloc] init];
    author.name    = @"Erickson";
    
    // Get the default Realm
    RLMRealm *realm = [RLMRealm defaultRealm];
    // You only need to do this once (per thread)
    
    // Add to Realm with transaction
    [realm beginWriteTransaction];
    [realm addObject:author];
    [realm commitWriteTransaction];

    等到把这个对象添加到realm数据库里面之后, 可以在多个线程里面共享之。并且从现在开始,所做的每一次更改(必须在一个写事务中完成)也会被永久储存。等到写事务完成,这个更改将对所有共享这个Realm数据库的线程可见。

    需要注意的是,写入操作会相互阻塞,而且其相对应的进程也会受到影响。这和其他的永久数据存储解决方案是一样的,所以建议你使用常用的,也是最有效的方案, 将所有写入放到一个单独的进程中。

    还要注意的是,因为realm的MVCC结构, 读取并不会因为一个进行中的写事务而受到影响。

posted @ 2015-06-04 12:21  Erickson  阅读(1087)  评论(0编辑  收藏  举报