一、环境配置
(1) 在项目的build文件(build.gradle(Project:xxx))加上
dependencies {
classpath"io.realm:realm-gradle-plugin:3.0.0"
}
(2) 在app的build文件(build.gradle(Module:app))加上
apply plugin: 'realm-android'
二、初始化Realm
(1) 在Application的oncreate()方法中使用默认配置
public class MyApplication extends Application {
private static MyApplication instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
//初始化Realm
Realm.init(this);
//把配置设置为默认配置
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(realmConfiguration);
}
// 获取Application
public static Context getMyApplication() {
return instance;
}
}
(2) 在Application的oncreate()方法中使用自定义配置
public class MyApplication extends Application {
private static MyApplication instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
//初始化Realm
Realm.init(this);
//RealmConfiguration支持的方法:
//Builder.name : 指定数据库的名称。如不指定默认名为default。
//Builder.schemaVersion : 指定数据库的版本号。
//Builder.encryptionKey : 指定数据库的密钥。
//Builder.migration : 指定迁移操作的迁移类。
//Builder.deleteRealmIfMigrationNeeded : 声明版本冲突时自动删除原数据库。
//Builder.inMemory : 声明数据库只在内存中持久化。
//build : 完成配置构建。
//自定义配置
RealmConfiguration config = new RealmConfiguration.Builder()
.name("myRealm.realm")
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
}
// 获取Application
public static Context getMyApplication() {
return instance;
}
}
(3)在AndroidManifest.xml配置自定义的Application
<application
android:name=".MyApplication"
...
/>
三、关闭Realm
在Activity中使用完后,需要在onDestroy()中关闭Realm
@Override
protected void onDestroy() {
super.onDestroy();
// Close the Realm instance.
realm.close();
}
四、创建实体
(1)新建一个类继承RealmObject
public class Dog extends RealmObject {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
(2)多对多关系
public class Contact extends RealmObject {
public String name;
public RealmList<Email> emails;
}
public class Email extends RealmObject {
public String address;
public boolean active;
}
(3)其他相关说明
1、支持的数据类型:
boolean, byte, short, int, long, float, double, String, Date and byte[]
在Realm中byte, short, int, long最终都被映射成long类型
2、注解说明
@PrimaryKey
①字段必须是String、 integer、byte、short、 int、long 以及它们的封装类Byte, Short, Integer, and Long
②使用了该注解之后可以使用copyToRealmOrUpdate()方法,通过主键查询它的对象,如果查询到了,则更新它,否则新建一个对象来代替。
③使用了该注解将默认设置@index注解
④使用了该注解之后,创建和更新数据将会慢一点,查询数据会快一点。
@Required
数据不能为null
@Ignore
忽略,即该字段不被存储到本地
@Index
为这个字段添加一个搜索引擎,这将使插入数据变慢、数据增大,但是查询会变快。建议在需要优化读取性能的情况下使用。
五、数据库操作
realm数据库的操作可以使用事务或事务块来执行,下面以增
(1)增
1、事务操作
a、新建一个对象并进行存储
//获取数据库实例
Realm realm=Realm.getDefaultInstance();
realm.beginTransaction();//开启事务
User user = realm.createObject(User.class); //新建对象
user.setName("John");
user.setEmail("john@corporation.com");
realm.commitTransaction();//提交事务
b、复制一个对象到realm
//获取数据库实例
Realm realm=Realm.getDefaultInstance();
//新建对象
User user = new User("John");
user.setEmail("john@corporation.com");
// Copy the object to Realm. Any further changes must happen on realmUser
realm.beginTransaction();//开启事务
realm.copyToRealm(user);//复制对象
realm.commitTransaction();//提交事务
2、使用事务块
//获取数据库实例
Realm mRealm=Realm.getDefaultInstance();
//新建对象
final User user = new User("John");
user.setEmail("john@corporation.com");
//使用事务块执行操作
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealm(user);
}
});
(2)删除
//获取数据库实例
Realm mRealm=Realm.getDefaultInstance();
//查询
final RealmResults<Dog> dogs= mRealm.where(Dog.class).findAll();
//使用事务块执行操作
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Dog dog=dogs.get(5);
dog.deleteFromRealm();
//删除第一个数据
dogs.deleteFirstFromRealm();
//删除最后一个数据
dogs.deleteLastFromRealm();
//删除位置为1的数据
dogs.deleteFromRealm(1);
//删除所有数据
dogs.deleteAllFromRealm();
}
});
(3)改
//获取数据库实例
Realm mRealm=Realm.getDefaultInstance();
//获取查询的对象实例
Dog dog = mRealm.where(Dog.class).equalTo("id", id).findFirst();
mRealm.beginTransaction();//开启事务
dog.setName(newName);//修改
mRealm.commitTransaction();//提交事务
(4)查
1、查询全部
查询结果为RealmResults<T>,可以使用mRealm.copyFromRealm(dogs)方法将它转为List<T>
Realm mRealm=Realm.getDefaultInstance();
RealmResults<Dog> dogs = mRealm.where(Dog.class).findAll();
List<Dog> list = mRealm.copyFromRealm(dogs);
2、条件查询
Realm mRealm=Realm.getDefaultInstance();
Dog dog = mRealm.where(Dog.class).equalTo("id", id).findFirst();
常见的条件如下(详细资料请查官方文档):
between(), greaterThan(), lessThan(), greaterThanOrEqualTo() & lessThanOrEqualTo()
equalTo() & notEqualTo()
contains(), beginsWith() & endsWith()
isNull() & isNotNull()
isEmpty() & isNotEmpty()
3、对查询结果进行排序
RealmResults<Dog> dogs = mRealm.where(Dog.class).findAll();
//增序排列
dogs=dogs.sort("id");
//降序排列
dogs=dogs.sort("id", Sort.DESCENDING);
4、其他查询
sum,min,max,average只支持整型数据字段
//查询平均年龄
double avgAge= mRealm.where(Dog.class).findAll().average("age");
// 查询总年龄
Number sum= mRealm.where(Dog.class).findAll().sum("age");
int sumAge=sum.intValue();
//查询最大年龄
Number max= mRealm.where(Dog.class).findAll().max("age");
int maxAge=max.intValue();
六、异步操作
大多数情况下,Realm的增删改查操作足够快,可以在UI线程中执行操作。但是如果遇到较复杂的增删改查,或增删改查操作的数据较多时,就可以子线程进行操作。
(1)异步增
private void addCat(final Cat cat) {
RealmAsyncTask addTask= mRealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealm(cat);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
ToastUtil.showShortToast(mContext,"收藏成功");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
ToastUtil.showShortToast(mContext,"收藏失败");
}
});
}
最后在销毁Activity或Fragment时,要取消掉异步任务
@Override
protected void onDestroy() {
super.onDestroy();
if (addTask!=null&&!addTask.isCancelled()){
addTask.cancel();
}
}
删除、修改和查询亦同理
七、数据迁移
方法一、删除旧版本的数据
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build()
方法二、设置schema 版本和 migration,对改变的数据进行处理
RealmConfiguration config = new RealmConfiguration.Builder()
.schemaVersion(2) // Must be bumped when the schema changes
.migration(new Migration()) // Migration to run instead of throwing an exception
.build()
处理版本数据变化Migration
public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
// During a migration, a DynamicRealm is exposed. A DynamicRealm is an untyped variant of a normal Realm, but
// with the same object creation and query capabilities.
// A DynamicRealm uses Strings instead of Class references because the Classes might not even exist or have been
// renamed.
// Access the Realm schema in order to create, modify or delete classes and their fields.
RealmSchema schema = realm.getSchema();
/************************************************
// Version 0
class Person
@Required
String firstName;
@Required
String lastName;
int age;
// Version 1
class Person
@Required
String fullName; // combine firstName and lastName into single field.
int age;
************************************************/
// Migrate from version 0 to version 1
if (oldVersion == 0) {
RealmObjectSchema personSchema = schema.get("Person");
// Combine 'firstName' and 'lastName' in a new field called 'fullName'
personSchema
.addField("fullName", String.class, FieldAttribute.REQUIRED)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.set("fullName", obj.getString("firstName") + " " + obj.getString("lastName"));
}
})
.removeField("firstName")
.removeField("lastName");
oldVersion++;
}
/************************************************
// Version 2
class Pet // add a new model class
@Required
String name;
@Required
String type;
class Person
@Required
String fullName;
int age;
RealmList<Pet> pets; // add an array property
************************************************/
// Migrate from version 1 to version 2
if (oldVersion == 1) {
// Create a new class
RealmObjectSchema petSchema = schema.create("Pet")
.addField("name", String.class, FieldAttribute.REQUIRED)
.addField("type", String.class, FieldAttribute.REQUIRED);
// Add a new field to an old class and populate it with initial data
schema.get("Person")
.addRealmListField("pets", petSchema)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
if (obj.getString("fullName").equals("JP McDonald")) {
DynamicRealmObject pet = realm.createObject("Pet");
pet.setString("name", "Jimbo");
pet.setString("type", "dog");
obj.getList("pets").add(pet);
}
}
});
oldVersion++;
}
/************************************************
// Version 3
class Pet
@Required
String name;
int type; // type becomes int
class Person
String fullName; // fullName is nullable now
RealmList<Pet> pets; // age and pets re-ordered (no action needed)
int age;
************************************************/
// Migrate from version 2 to version 3
if (oldVersion == 2) {
RealmObjectSchema personSchema = schema.get("Person");
personSchema.setNullable("fullName", true); // fullName is nullable now.
// Change type from String to int
schema.get("Pet")
.addField("type_tmp", int.class)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
String oldType = obj.getString("type");
if (oldType.equals("dog")) {
obj.setLong("type_tmp", 1);
} else if (oldType.equals("cat")) {
obj.setInt("type_tmp", 2);
} else if (oldType.equals("hamster")) {
obj.setInt("type_tmp", 3);
}
}
})
.removeField("type")
.renameField("type_tmp", "type");
oldVersion++;
}
}
}
转自:
https://www.jianshu.com/p/28912c2f31db
网友评论