Exception handling for android realm. Cannot modify managed objects outside of a write transaction

Asked 2 years ago, Updated 2 years ago, 147 views

Hello, this is an exception to realm.
I'd like to set a random value of int type randomId to an instance of a model class called Word.

java.lang.IllegalStateException:Cannot modify managed objects outside of a write transaction.

It causes to occur.Why?

I tried to put any number in setRandomId() instead of random, or to use a method (which uses a method called setMeaning but omits the content) to set a value that works fine with other activities, but the same error statement appears.
Due to the stack overflow, there was no information about this exception handling on Android, so I asked you.

private void setRandom(){
    mEditor.putBoolean("random", true);
    mEditor.apply();

    mRealm=Realm.getDefaultInstance();
    RealmResults<Word>realmResults=mRealm.where(Word.class).findAll();
    intrealmSize=realmResults.size();
    mRealm.close();

    boolean num[] = new boolean [realmSize]; // for duplicate determination
    Random random=new Random(); // Random number

    // Keep all duplicate sequences false
    for(inti=0;i<realmSize;i++){
        num[i] = false;
    }

    for(inti=0;i<realmSize;){

        intp = land.nextInt(realmSize);

        if(!num[p]){
            Word=mRealm.where(Word.class).equalTo("id", i).findFirst();
            word.setRandomId(p);// This is where the crash occurs
            num[p] = true;
            i++;
            mRealm.beginTransaction();
            mRealm.copyToRealmOrUpdate(word);
            mRealm.commitTransaction();

            mRealm.close();
        }
    }
}

android realm

2022-09-30 21:31

1 Answers

Cannot modify managed objects outside of a write transaction.

The error message is correct.Any changes to the properties of objects stored in Realm (=managed objects) must be made in a transaction.
(Objects before saving to Realm are just objects, not just objects)

The word object in the code below is a Managed object because it was retrieved from Realm in mRealm.where(Word.class).equalTo("id", i).findFirst();.Changes must be made within the transaction.

 if(!num[p]){
    Word=mRealm.where(Word.class).equalTo("id", i).findFirst();
    word.setRandomId(p);// This is where the crash occurs
    num[p] = true;
    i++;
    mRealm.beginTransaction();
    mRealm.copyToRealmOrUpdate(word);
    mRealm.commitTransaction();

    mRealm.close();
}

Expand the scope of the transaction to make it easier:

 if(!num[p]){
    mRealm.beginTransaction();

    Word=mRealm.where(Word.class).equalTo("id", i).findFirst();
    word.setRandomId(p);// This is where the crash occurs
    num[p] = true;
    i++;

    mRealm.copyToRealmOrUpdate(word);
    mRealm.commitTransaction();

    mRealm.close();
}

However, this process is done in the For loop, so if there is no particular reason, it is better to extend the transaction to the outside of the loop.Committing a transaction is a fairly expensive process, so performance will be poor if you start and commit every transaction in the loop.


2022-09-30 21:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.