在Android中使用Singleton模式的领域:是否正确的结构?

时间:2018-03-25 17:09:40

标签: android singleton realm realm-mobile-platform

目前,我的代码中有singleton结构用于管理Realm交易。我需要知道以下单例结构的优缺点。通过这种方法,我将从我的所有活动和片段中将updateClockModel()称为RealManager.getInstance().updateClockModel(...)

public class RealmManager {

private static final String TAG = "RealmManager";
private static RealmManager mInstance = null;
private final ThreadLocal<Realm> localRealm = new ThreadLocal<>();

public static RealmManager getInstance() {
    if (mInstance == null)
        mInstance = new RealmManager();
    return mInstance;
}

public Realm openLocalInstance() {
    Realm realm = Realm.getDefaultInstance();
    if (localRealm.get() == null) {
        localRealm.set(realm);
    }
    return realm;
}

public Realm getLocalInstance() {
    Realm realm = localRealm.get();
    if (realm == null) {
        throw new IllegalStateException("No open Realms were found on this thread.");
    }
    return realm;
}

public void closeLocalInstance() {
    Realm realm = localRealm.get();
    if (realm == null) {
        throw new IllegalStateException(
                "Cannot close a Realm that is not open.");
    }
    realm.close();
    if (Realm.getLocalInstanceCount(Realm.getDefaultConfiguration()) <= 0) {
        localRealm.set(null);
    }
}

protected RealmManager() {
}

public void updateClockModel(ClockRLM clockRLM, OnRealmDatabaseListener mRealmListener) {
    Realm mRealm = openLocalInstance();

    mRealm.executeTransactionAsync(realm -> {
        RealmResults<ClockRLM> result = realm.where(ClockRLM.class).equalTo("timeStamp", clockRLM.getTimeStamp()).findAll();
        for (ClockRLM clockRLM1 : result) {
            clockRLM1.setUploadedSuccess(true);
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            Log.d("Clocke ", "inserted TimeStamp " + clockRLM.getTimeStamp());
            if (mRealmListener != null)
                mRealmListener.isDatabaseOperationSuccess(clockRLM, true);

            closeLocalInstance();
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            if (mRealmListener != null)
                mRealmListener.isDatabaseOperationSuccess(clockRLM, false);
            closeLocalInstance();
        }
    });
}

public void addClockModel(ClockRLM clockRLM, OnRealmDatabaseListener mRealmListener) {
    Realm mRealm = openLocalInstance();

    mRealm.executeTransactionAsync(realm -> realm.copyToRealm(clockRLM), new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            Log.d("Clocke ", "Inserted TimeStamp " + clockRLM.getTimeStamp());
            if (mRealmListener != null)
                mRealmListener.isDatabaseOperationSuccess(clockRLM, true);
            closeLocalInstance();
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            closeLocalInstance();
        }
    });
  }
}

1 个答案:

答案 0 :(得分:1)

它会工作,除了那些写入的方法不能在后台线程上执行 - 只在ui线程上 - 所以我添加类似下面的方法

private void executeInTransaction(Realm.Transaction transaction) {
    try {
        Realm realm = openLocalInstance();
        if(!realm.isAutoRefresh()) {
            try {
                boolean wasInTransaction = realm.isInTransaction();
                if(!wasInTransaction) {
                    realm.beginTransaction();
                }
                transaction.execute(realm);
                if(!wasInTransaction) {
                    realm.commitTransaction();
                }
            } catch(Throwable e) {
                if(realm.isInTransaction()) {
                    realm.cancelTransaction();
                }
            }
        } else {
            realm.executeTransactionAsync(transaction);
        }
    } finally {
        closeLocalInstance();
    }
}

这样,您可以通过手动事务打开来执行批处理后台操作,并从UI线程执行异步写入。

您需要进行一些调整才能添加成功/失败&#34;听众,但基础知识在那里。