How to close realm opened by Realm.getDefaultInstance?

柔情痞子 提交于 2019-12-05 20:05:44

问题


I am using realm to store and retrieve data. Usually when we open a realm to store some data we do like:

Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
// Copy the object to Realm
realm.copyToRealm(myObject);
realm.commitTransaction();
realm.close();

in the above case I am closing the realm.

But when I am retrieving some data like:

RealmResults<MyClass> results = Realm.getDefaultInstance().where(MyClass.class).findAll();

How do I close this realm? Does it need to be closed?


回答1:


Doing it as a one-liners means you cannot close the Realm, so I would advice against that.

Not closing the Realm will in the best case cause you to leak memory and have a higher chance of being killed by the system if the in the background. Worst case you will see a high increase in disk usage because Realm has to keep track of all versions of opened Realm instances (due to being a MVCC database).

I would highly advice using your first pattern. For more information about controlling the Realm instances you can read this: https://realm.io/docs/java/latest/#realm-instance-lifecycle and this https://realm.io/news/threading-deep-dive/



来源:https://stackoverflow.com/questions/37113078/how-to-close-realm-opened-by-realm-getdefaultinstance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!