I\'m using a setup in which every Presenter
that is a retained Fragment
has its own Realm
instance. However, this essentially means th
Realm uses a ThreadLocal cache internally pr. Realm file so it is practically free to call Realm.getInstance()
in every activity/fragment/presenter you have. The first call to Realm.getInstance()
will cost a little as database has to be opened and the schema verified, but after that it just cost a cache lookup.
The cache is reference counted so the native resources will only be freed after all instances has been closed. This means it can be beneficial to keep at least one open instance around as long as possible.
This also means that when you update 1 of you open instances, they all get updated automatically.
A possible way of having an open realm at all times while the application is active.
public class BaseActivity extends AppCompatActivity {
private CustomApplication customApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customApplication = (CustomApplication)getApplication();
if(savedInstanceState == null) {
customApplication.incrementActivityCounter();
}
}
@Override
protected void onDestroy() {
if(isFinishing()) {
customApplication.decrementActivityCounter();
}
super.onDestroy();
}
}
public class CustomApplication extends Application {
public static final String TAG = CustomApplication.class.getSimpleName();
private volatile int activityCounter = 0;
private Realm realm;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Application onCreate() called.");
initializeRealm();
}
public void incrementActivityCounter() {
if (activityCounter == 0) {
Log.d(TAG, "Increment: Activity counter was 0, initializing Realm.");
if(realm == null) {
initializeRealm();
}
}
activityCounter++;
Log.d(TAG, "Increment: Activity counter incremented to " + activityCounter + ".");
}
public void decrementActivityCounter() {
activityCounter--;
Log.d(TAG, "Decrement: Activity counter decremented to " + activityCounter + ".");
if(activityCounter == 0) {
realm.close();
realm = null;
Log.d(TAG, "Decrement: Activity counter was 0, closed realm.");
}
}
private void initializeRealm() {
realm = Realm.getInstance(this);
Log.d(TAG, "Realm initialized.");
}
public Realm getRealm() {
return realm;
}
public int getActivityCounter() {
return activityCounter;
}
public void setActivityCounter(int activityCounter) {
this.activityCounter = activityCounter; //process death
}
}
And then
public class BaseActivity
extends AppCompatActivity {
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("activityCounter", ((CustomApplication) getApplication()).getActivityCounter());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int activityCounter = savedInstanceState.getInt("activityCounter");
((CustomApplication) getApplication()).setActivityCounter(activityCounter); //fix process death initializing activity counter to 0
Log.d(TAG, "Reset activity counter in application after process death to [" + activityCounter + "]");
}
}