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

匿名 (未验证) 提交于 2019-12-03 00:50:01

问题:

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;     }  } 

回答1:

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;    } } 


回答2:

I have a helper class named RealmHelper and above is one of its methods.

 public static void saveObject(RealmObject objectToSave) {      Realm realm = getRealm();     realm.beginTransaction();     realm.copyToRealmOrUpdate(objectToSave);     realm.commitTransaction(); } 

Bacause of this, my code structure is this, no realm methods or object shouldn't be in activity or fragment.

For overcoming this error, everytime i create new object with same id, and realm can overwrite old object.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!