使用Realm保留对象(错误:更改Realm数据只能在事务内部完成)

时间:2022-11-20 09:44:09

I am having difficulties getting Realm to work.

我很难让Realm工作。

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

错误:java.lang.IllegalStateException:更改Realm数据只能从事务内部完成

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

叫我疯了,但不是我在交易中做数据持久性,这究竟是如何在文档中完成的?请在此处使用copyToRealm查看示例: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.

编辑:什么,Realm不支持自动增加主键ID?对我来说是违规的。我会留下这个问题,以防它帮助其他人。

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

}

2 个解决方案

#1


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

1.)你实际上是在多个地方调用Realm.getDefaultInstance(),从而修改一个实际上没有设置在事务中的Realm实例

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

2.)您的领域对象上没有默认的空构造函数


Here is some canonical usage example:

这是一些规范用法示例:

  • UI thread:
  • UI线程:

.

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


0  

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

我有一个名为RealmHelper的辅助类,上面是它的一个方法。

 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.

为了克服这个错误,每次我创建具有相同id的新对象,并且realm可以覆盖旧对象。

#1


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

1.)你实际上是在多个地方调用Realm.getDefaultInstance(),从而修改一个实际上没有设置在事务中的Realm实例

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

2.)您的领域对象上没有默认的空构造函数


Here is some canonical usage example:

这是一些规范用法示例:

  • UI thread:
  • UI线程:

.

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


0  

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

我有一个名为RealmHelper的辅助类,上面是它的一个方法。

 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.

为了克服这个错误,每次我创建具有相同id的新对象,并且realm可以覆盖旧对象。