如何关闭由Realm.getDefaultInstance打开的领域? [英] How to close realm opened by Realm.getDefaultInstance?

查看:258
本文介绍了如何关闭由Realm.getDefaultInstance打开的领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用领域来存储和检索数据.通常,当我们打开一个领域来存储一些数据时,我们会这样做:

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?

推荐答案

以单线形式进行操作意味着您无法关闭Realm,因此我建议您不要这样做.

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

在最佳情况下,不关闭Realm会导致您泄漏内存,并且如果在后台,则更有可能被系统杀死.最坏的情况是,由于Realm必须跟踪打开的Realm实例的所有版本(由于是MVCC数据库),因此磁盘使用率会大大增加.

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).

我强烈建议您使用第一种模式.有关控制Realm实例的更多信息,您可以阅读以下内容: https://realm.io/docs/java/latest/#realm-instance-lifecycle 和此 https://realm.io/news/threading-deep-dive/

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/

这篇关于如何关闭由Realm.getDefaultInstance打开的领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆