Realm和Sqlite类似,都是以文件数据库的方式而存在,一个Realm相当于一个SQLite数据库。它有一个与之对应的文件,一旦创建将持久保存在安卓的文件系统中。要创建一个新的Realm,你可以在任意Activity中调用静态方法Realm.getInstance
Realm myRealm = Realm.getInstance(context);
注意,调用Realm.getInstance,而没有传入RealmConfiguration,会创建一个叫做 default.realm的Realm文件。
如果你想向app中添加另一个Realm,必须使用一个RealmConfiguration.Builder对象,并为 Realm file 指定一个独有的名字。
Realm myOtherRealm = Realm.getInstance(new RealmConfiguration.Builder(context) .name("myRealm.realm") .build());
创建ORM对象
public class Country extends RealmObject { private String name; private int population; @PrimaryKey //添加主键 private String code; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Country() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPopulation() { return population; } public void setPopulation(int population) { this.population = population; } }
虽然从一个Realm读取数据非常简单(下一节有讲),但是向它写入数据就稍微复杂一点。Realm遵循 (数据库事务正确执行的四个基本要素的缩写)规范,为了确保原子性和一致性,它强制所有的写入操作都在一个事务中执行。
要开始一个新的事务,使用beginTransaction方法。类似地,要结束这个事务,使用commitTransaction方法。
注:事务即英文里面的transaction。
这里演示了如何创建和保存一个Country类的实例:
myRealm.beginTransaction(); // Create an object Country country1 = myRealm.createObject(Country.class); // Set its fields country1.setName("Norway"); country1.setPopulation(5165800); country1.setCode("NO"); myRealm.commitTransaction();
或者如下进行事务
Country country2 = new Country();country2.setName("Russia");country2.setPopulation(146430430);country2.setCode("RU"); myRealm.beginTransaction();Country copyOfCountry2 = myRealm.copyToRealm(country2);myRealm.commitTransaction();
当然查询也有很多api
RealmResultsresults1 = myRealm.where(Country.class).findAll();
再来看看官方的api
Dog类
public class Dog extends RealmObject { @Required //字段不能为null private String name; private int age; //getter,setter方法 }
Person类
public class Person extends RealmObject { @Required //字段不能为null private String name; // imageUrl 是可选字段 private String imageUrl; private RealmListdogs; // 一对多的关系 // getter,setter方法 }
普通用法
Dog dog = new Dog(); dog.setName("Rex"); dog.setAge(1); Log.v(TAG, "Name of the dog: " + dog.getName()); //使用建造者模式构建一个配置 RealmConfiguration realmConfig = new RealmConfiguration.Builder(context).build(); //通过配置创建(或打开已存在的)数据库 Realm realm = Realm.getInstance(realmConfig); //通过连贯语句进行 RealmResultspuppies = realm.where(Dog.class).lessThan("age", 2).findAll(); puppies.size();
事务操作
第一种事务操作方式
realm.beginTransaction(); realm.copyToRealm(dog); realm.commitTransaction();
第二种事务操作方式(异步线程执行)
// Queries are updated in real time puppies.size(); // => 1 // Query and update the result asynchronously in another thread realm.executeTransactionAsync(new Realm.Transaction() { @Override public void execute(Realm realm) { Dog theDog = realm.where(Dog.class).equals("age", 1).findFirst(); theDog.setAge(3); } }, new Realm.Transaction.Callback() { @Override public void onSuccess() { //自动更新原始数据,如puppies.size(); // => 0 because there are no more puppies (less than 2 years old) dog.getAge(); // => 3 the dogs age is updated } });
当然,官方的Api也支持RxJava这里就不多说了
官方文档: