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