Refreshing multiple Realm instances at once?

落花浮王杯 提交于 2019-11-29 15:34:01

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 + "]");
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!