How to keep one Realm instance throughout the entire application lifecycle and also close it?

How can I maintain one Realm instance throughout the entire application lifecycle and also close it.

I can achieve instance support using a singleton class, but how do I close it when the application is closed?

Also, is it safe to close the Realm instance after opening?

+3


source to share


2 answers


I am using singleton RealmManager

for UI thread and for background threads I open / close Realm with block try-with-sources

.

So, for the UI thread:

public class RealmManager {
    private static final String TAG = "RealmManager";

    static Realm realm;

    static RealmConfiguration realmConfiguration;

    public static void initializeRealmConfig(Context appContext) {
        if(realmConfiguration == null) {
            Log.d(TAG, "Initializing Realm configuration.");
            setRealmConfiguration(new RealmConfiguration.Builder(appContext).initialData(new RealmInitialData())
                    .deleteRealmIfMigrationNeeded()
                    .inMemory()
                    .build());
        }
    }

    public static void setRealmConfiguration(RealmConfiguration realmConfiguration) {
        RealmManager.realmConfiguration = realmConfiguration;
        Realm.setDefaultConfiguration(realmConfiguration);
    }

    private static int activityCount = 0;

    public static Realm getRealm() {
        return realm;
    }

    public static void incrementCount() {
        if(activityCount == 0) {
            if(realm != null) {
                if(!realm.isClosed()) {
                    Log.w(TAG, "Unexpected open Realm found.");
                    realm.close();
                }
            }
            Log.d(TAG, "Incrementing Activity Count [0]: opening Realm.");
            realm = Realm.getDefaultInstance();
        }
        activityCount++;
        Log.d(TAG, "Increment: Count [" + activityCount + "]");
    }

    public static void decrementCount() {
        activityCount--;
        Log.d(TAG, "Decrement: Count [" + activityCount + "]");
        if(activityCount <= 0) {
            Log.d(TAG, "Decrementing Activity Count: closing Realm.");
            activityCount = 0;
            realm.close();
            if(Realm.compactRealm(realmConfiguration)) {
                Log.d(TAG, "Realm compacted successfully.");
            }
            realm = null;
        }
    }
}

      



And for the background thread:

try(Realm realm = Realm.getDefaultInstance()) {
   // ...
}

      

+2


source


Why don't you create a wrapping class for your realm instance (could be a singleton) and then add some methods to it so that instead of closing the scope every time you can just call your own method and close the used instance as soon as will you finish? Something like that.



public class WrappingRealm {
    public static WrappingRealm getInstance() {
        //create your singleton here. Be aware of synchronization issues
    }

    private Realm getRealm() {
        return Realm.getDefaultInstance();
    }

    public void save(RealmModel obj)  {
        Realm currentRealm = getRealm();

        currentRealm.executeTransaction {
            //Do your stuff
        }

        currentRealm.close();
    }
}

      

0


source







All Articles