Android 数据库之 Realm(四)
一、简介
Realm 是
YCombinator
团队专门针对移动平台设计的数据库,目前支持 IOS、 Android平台,支持Objective-C、 Swift、Java、 React Native等编程语言。该数据库没有采用 SQLite 数据库,而是使用独立的内部数据引擎C++开发的。
- 开源:目前 Realm 相关代码已全部开源,有社区有团队对其进行补充扩展;
- 简单易用:API 相对简单,集成方便;
- 跨平台:兼容 IOS 和 Android;
- 可视化:对应提供一个轻量级的数据库查看工具 Realm Browser。
二、集成
- 在项目级别的
build.gradle
中:
buildscript {
dependencies {
//Realm依赖
classpath 'io.realm:realm-gradle-plugin:5.8.0'
}
}
- 在模块级别(如
app
模块)的build.gradle
中:
apply plugin: 'com.android.application'
// 或者 apply plugin: 'com.android.library'
apply plugin: 'realm-android'
- 引入 Java Bean 类
public class SimpleEntity extends RealmObject {
@PrimaryKey
private long id;
private boolean simpleBoolean;
private byte simpleByte;
private short simpleShort;
private int simpleInt;
private long simpleLong;
private float simpleFloat;
private double simpleDouble;
private String simpleString;
private byte[] simpleByteArray;
}
三、封装Realm示例
四、示例代码
public class RealmUtils {
private static Map<String, BaseDao> daoManagers;
@SuppressLint("StaticFieldLeak")
private static Context context;
public static void init(Context context) {
RealmUtils.context = context.getApplicationContext();
if (daoManagers == null) {
daoManagers = new ConcurrentHashMap<>();
}
}
public static SimpleEntityManager getSimpleEntityManager() {
SimpleEntityManager manager = (SimpleEntityManager) daoManagers.get(SIMPLE_ENTITY);
if (manager == null) {
manager = new SimpleEntityManager(context);
daoManagers.put(SIMPLE_ENTITY, manager);
}
return manager;
}
public static void closeAllDataBase() {
if (daoManagers != null && !daoManagers.isEmpty()) {
for (BaseDao baseDao : daoManagers.values()) {
baseDao.closeDataBase();
}
daoManagers = null;
}
}
public static boolean deleteAllFiles() {
boolean deleteNative = RealmManager.getInstance().deleteAllFiles();
return deleteNative;
}
}
public class SimpleEntityManager extends BaseDao<SimpleEntity> {
public SimpleEntityManager(Context context) {
super(context);
}
public long getAllCount() {
return mRealm.where(SimpleEntity.class).count();
}
}
public class BaseDao<T extends RealmObject> {
protected RealmManager mManager;
protected Realm mRealm;
public BaseDao(Context context) {
mManager = RealmManager.getInstance();
mManager.init(context);
mRealm = mManager.getRealm();
}
// 增、删、改、查
}
// 增
public boolean insertObject(T object) {
boolean flag = false;
try {
mRealm.insert(object);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
// 删
public boolean deleteObject(final T object) {
boolean flag = false;
if (null == object) {
return flag;
}
try {
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
object.deleteFromRealm();
}
});
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
// 改
public void updateObject(final T object) {
if (null == object) {
return;
}
try {
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.insertOrUpdate(object);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
// 查
public List<T> queryAll(Class<T> object) {
List<T> objects = null;
try {
objects = mRealm.where(object).findAll();
} catch (Exception e) {
e.printStackTrace();
}
return objects;
}
五、使用测试
注:可能因为具体测试机器不同,测试结果会有波动。下面的测试结果是用HuaWei Mate10测试的。
七、地址
https://github.com/HongdouHub/testorm