Realm DB在Android中疯狂

时间:2017-07-21 13:31:33

标签: java android realm

所以我有以下问题:

    if (!realm.isClosed()){
        Log.d("PROVIDER realm not clsd", "closing now");
        realm.executeTransaction((t)->realm.close());
    }

这会引发异常。这是:

D/PROVIDER realm not clsd: closing now
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver This Realm instance has already been closed, making it unusable.
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3047)

现在,当"Realm instance has already been closed"

时,地狱是如何realm.isClosed()==false

1 个答案:

答案 0 :(得分:3)

在您检查realm.isClosed()时,它确实没有关闭。但是,您已经打开了一个事务,为了完成该事务,仍然需要打开realm实例。问题是,您在事务中关闭此实例。 简单解决方案:从交易中删除realm.close(),它不需要在一个。