Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persisting objects with Realm (error: Changing Realm data can only be done from inside a transaction)

I am having difficulties getting Realm to work.

RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfig);
Realm realm = Realm.getDefaultInstance();

MyObjectExtendingRealmObject myObject = new MyObjectExtendingRealmObject("John");
realm.beginTransaction();
realm.copyToRealm(myObject);
realm.commitTransaction();

Error: java.lang.IllegalStateException: Changing Realm data can only be done from inside a transaction

Call me crazy but aren't I doing the data persistence inside the transaction, which is exactly how it's done in the docs? See the example using copyToRealm here: https://realm.io/docs/java/latest/#creating-objects

What am I doing wrong?

Edit: What, Realm doesn't support autoincrementing primary key ids? Dealbreaker for me. I'll leave this question up in case it helps others though.

Edit: My class

public class MyObjectExtendingRealmObject extends RealmObject {
    private String name;

    public MyObjectExtendingRealmObject() {

    }

    public MyObjectExtendingRealmObject(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
like image 836
AJJ Avatar asked Sep 23 '16 02:09

AJJ


1 Answers

There are two possible reasons I can see for this:

1.) you're actually calling Realm.getDefaultInstance() in multiple places and thus tinkering with a Realm instance that wasn't actually set to be in a transaction

2.) you don't have a default empty constructor on your realm object


Here is some canonical usage example:

  • UI thread:

.

Realm realm;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    realm = Realm.getDefaultInstance();
    realm.executeTransactionAsync(new Realm.Transaction() {
        public void execute(Realm realm) {
            MyObjectExtendingRealmObject myObject = new MyObjectExtendingRealmObject("John");
            realm.insertOrUpdate(myObject); // could be copyToRealmOrUpdate
        }
    });
}

@Override
public void onDestroy() {
    super.onDestroy();
    if(realm != null) { // don't trust old devices
        realm.close();
    }
}
  • background thread:

.

Realm realm = null;
try {
   realm = Realm.getDefaultInstance();
   realm.executeTransaction(new Realm.Transaction() {
       @Override
       public void execute(Realm realm) {
            MyObjectExtendingRealmObject myObject = new MyObjectExtendingRealmObject("John");
            realm.insertOrUpdate(myObject); // could be copyToRealmOrUpdate
       }
   });
} finally {
    if(realm != null) {
        realm.close();
    }
}

Realm model:

public class MyObjectExtendingRealmObject extends RealmObject {
   public MyObjectExtendingRealmObject() { // needed
   }

   public MyObjectExtendingRealmObject(String name) {
       this.name = name;
   }

   @PrimaryKey
   private String name;

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
}
like image 55
EpicPandaForce Avatar answered Oct 13 '22 17:10

EpicPandaForce