1. 程式人生 > >Realm資料庫的基本操作

Realm資料庫的基本操作

public class BaseRealmDao {

    private Realm realm;
    public BaseRealmDao(Realm realm) {
        this.realm = realm;
}

    /**
     * 新增(效能優於下面的saveOrUpdate()方法)
     *
     * @param object
* @return 儲存或者修改是否成功
*/
public boolean insert(RealmObject object) {
        try {
            realm.beginTransaction();
realm.insert(object); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } /** * 新增(效能優於下面的saveOrUpdateBatch()方法) * * @param list * @return 批量儲存是否成功 */ public boolean
insert(List<? extends RealmObject> list) { try { realm.beginTransaction(); realm.insert(list); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } /** *
新增或者修改(效能優於下面的saveOrUpdate()方法) * * @param object * @return 儲存或者修改是否成功 */ public boolean insertOrUpdate(RealmObject object) { try { realm.beginTransaction(); realm.insertOrUpdate(object); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } /** * 新增或者修改(效能優於下面的saveOrUpdateBatch()方法) * * @param list * @return 儲存或者修改是否成功 */ public boolean insertOrUpdateBatch(List<? extends RealmObject> list) { try { realm.beginTransaction(); realm.insertOrUpdate(list); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } /** * 新增或者修改 * * @param object * @return 已經儲存的物件 */ public <E extends RealmObject> E saveOrUpdate(E object) { E savedE = null; try { realm.beginTransaction(); savedE = realm.copyToRealmOrUpdate(object); realm.commitTransaction(); return savedE; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return savedE; } } /** * 批量新增或者修改 * * @param list * @return 全部儲存成功 或 全部失敗 */ public boolean saveOrUpdateBatch(List<? extends RealmObject> list) { try { realm.beginTransaction(); realm.copyToRealmOrUpdate(list); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } /** * save or update RealmObject from json data * * @param jsonObject json資料 * @param clazz 具體型別 * @return 已經儲存的物件 */ public RealmObject saveOrUpdateFromJSON(Class<? extends RealmObject> clazz, String jsonObject) { RealmObject RealmObject = null; try { realm.beginTransaction(); RealmObject = realm.createOrUpdateObjectFromJson(clazz, jsonObject); realm.commitTransaction(); return RealmObject; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return RealmObject; } } /** * batch save or update from json array * * @param json json陣列 * @param clazz 型別 * @return 批量儲存json物件是否成功 */ public boolean saveOrUpdateFromJSONBatch(Class<? extends RealmObject> clazz, JSONArray json) { try { realm.beginTransaction(); realm.createOrUpdateAllFromJson(clazz, json); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } /** * 刪除當前表中所有資料 * * @param clazz * @return */ public boolean deleteAll(Class<? extends RealmObject> clazz) { try { realm.beginTransaction(); realm.delete(clazz); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } /** * 按照id刪除制定的記錄 * * @param clazz 型別 * @param idFieldName id欄位的名稱 * @param id id欄位值 * @return */ public boolean deleteById(Class<? extends RealmObject> clazz, String idFieldName, int id) { try { realm.beginTransaction(); realm.where(clazz).equalTo(idFieldName, id).findAll().deleteFirstFromRealm(); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } /** * 查詢所有 * * @return 返回結果集合 */ public RealmResults<? extends RealmObject> findAll(Class<? extends RealmObject> clazz) { return realm.where(clazz).findAll(); } /** * 清空資料庫 * * @return */ public boolean clearDatabase() { try { realm.beginTransaction(); realm.deleteAll(); realm.commitTransaction(); return true; } catch (Exception e) { e.printStackTrace(); realm.cancelTransaction(); return false; } } }