I'm currently using Realm in my application, and to make sure i manage Realm instances correctly, i introduced in my base Activity a variable like this:
protected val realm: Realm by lazy {
Realm.getDefaultInstance()
}
And then in onDestroy i do this:
override fun onDestroy() {
super.onDestroy()
realm.close()
}
And then i realised this is a waste. If the current activity doesn't use realm, it will open and immediately close in onDestroy
.
So i updated to this:
private var usedRealm = false
protected val realm: Realm by lazy {
usedRealm = true
Realm.getDefaultInstance()
}
override fun onDestroy() {
super.onDestroy()
if (usedRealm) {
realm.close()
}
}
Is there any way to accomplish the same, without the extra flag?