I'm using Realm
, a framework used for creating persistent objects on mobile devices. Realm
does not support nested transactions. So the problem is: I have a class with a static block that executes a Realm
transaction, however, the first time the class is used in my code is by one of its static methods from within another Realm
transaction. The error I'm getting is a java.lang.ExceptionInInitializerError
, and it is caused by this:
Caused by: java.lang.IllegalStateException: Nested transactions are not allowed. Use commitTransaction() after each beginTransaction().
And the line of code that it brings me to that's causing the error is the line in the static block that is executing the transaction.
Because the first time the class is used is in a Realm
transaction, it seems like the static block is not executed until that point, which is why I get the error caused by the nested transactions.
So just to clarify, it doesn't matter what type of variables a class has: static, static final, or whether they're initialized as instance variables or in a static block. Those variables will not be initialized until the class is interacted with for the first time. Correct?
Update:
Here is the code that occurred in the static block:
RealmSingleton.getUserInstance().executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
users.deleteAllFromRealm();
}
});