java 你如何检查一个领域实例是否已经关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29661201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you check if a realm instance has been closed already?
提问by zongweil
I'm using Realm for Android to store some data. When the user presses the log out button, I'd like to clear my entire Realm database. To do that, I have the following snippet of code:
我正在使用 Realm for Android 来存储一些数据。当用户按下注销按钮时,我想清除整个 Realm 数据库。为此,我有以下代码片段:
realm.close();
realm.deleteRealmFile(this);
goToLoginActivity();
The issue now is in my onResume function. I'm getting the following exception:
现在的问题出在我的 onResume 函数中。我收到以下异常:
Caused by: java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
引起:java.lang.IllegalStateException:这个 Realm 实例已经被关闭,使其无法使用。
My onResume code is as follows:
我的 onResume 代码如下:
@Override
protected void onResume() {
super.onResume();
// I'm trying to check if the realm is closed; this does not work
if (realm == null) {
realm = Realm.getInstance(this);
}
// Do query using realm
}
How can I check to see if a realm object has been closed already? Alternatively, is there a better way to clear the database than deleting the realm file?
如何检查领域对象是否已关闭?或者,有没有比删除领域文件更好的方法来清除数据库?
--
——
Edit: Just saw How can I easily delete all objects in a Realmfor iOS. Any word on when the deleteAllObjects API will be available for Android? At the time of writing, Android is at version 0.80.0 and the API is available in iOS in 0.87.0.
编辑:刚刚看到如何轻松删除 Realmfor iOS 中的所有对象。关于 deleteAllObjects API 何时可用于 Android 的任何消息?在撰写本文时,Android 的版本为 0.80.0,该 API 可在 0.87.0 的 iOS 中使用。
采纳答案by grim
To check if the realm instance is closed, use Realm's method isClosed:
要检查领域实例是否已关闭,请使用领域的方法isClosed:
if(realm.isClosed()) {
// Do something
}
To delete all Realm objects without deleting the realm file, you can use Realm's method clear; unfortunately you have to pass the RealmObject's class:
要删除所有 Realm 对象而不删除领域文件,可以使用 Realm 的方法clear;不幸的是,您必须通过 RealmObject 的类:
Realm realm = Realm.getInstance(context);
realm.beginTransaction();
realm.clear(SomeClass.class);
realm.clear(AnotherClass.class);
realm.commitTransaction();
realm.close();
回答by Christian Melchior
RealmObjects has a isValid()
method which will return false if the object has been deleted in the database or the Realm is closed, eg.
RealmObjects 有一个isValid()
方法,如果对象已在数据库中删除或领域已关闭,则该方法将返回 false,例如。
Realm realm = Realm.getInstance(getContext());
Foo foo = realm.allObjects(Foo.class).findFirst();
if (foo.isValid()) {
// Do something with Foo
} else {
// Any operation will throw a IllegalStateException
}
Delete all
删除所有
The android API has a Realm.clear(Class clazz)
method that does the same thing. http://realm.io/docs/java/api/io/realm/Realm.html#clear-java.lang.Class-This is to mimic the rest of the Collection API's but I can see the confusion in regards to the iOS API.
android API 有一个Realm.clear(Class clazz)
方法可以做同样的事情。http://realm.io/docs/java/api/io/realm/Realm.html#clear-java.lang.Class-这是为了模仿 Collection API 的其余部分,但我可以看到关于IOS API。
回答by TruMan1
Instead, do this:
相反,请执行以下操作:
if (realm != null) {
realm.close();
realm = null;
}
That way, you don't have to validate if the realm object is closed, but only check if it's null.
这样,您不必验证领域对象是否已关闭,而只需检查它是否为空。