境界异常'值'是不是有效的管理对象境界、异常、对象、有效

2023-09-06 23:43:29 作者:记忆里的那①天

我设置与另一个境界对象,它是不同类的领域对象上的属性,但是我得到的错误:值是不是avalid管理对象

I'm setting a property on a realm object with another realm object which is a different class, however I'm getting the error: 'value' is not avalid managed object.

realmObject.setAnotherRealmObject(classInstance.returnAnotherRealmObjectWithValues())

类实例接收anotherRealmObject构造,并与小部件值返回它通过方式:

The class instance receives anotherRealmObject constructor and returns it through the method with values from widgets:

public ClassInstance(AnotherRealmObject anotherRealmObject){
  mAnotherRealmObject = anotherRealmObject;
}

public AnotherRealmObject returnAnotherRealmObjectWithValues(){
       mAnotherRealmObject.setId(RandomUtil.randomNumbersAndLetters(5));
       mAnotherRealmObject.setName(etName.getText().toString());

       return mAnotherRealmObject;
}

我在创建新的另一种境界对象以正确的方式(我认为):

I'm creating the new Another Realm Object the right way (I think):

mAnotherRealmObject = mRealmInstance.createObject(AnotherRealmObject.class);

是不是因为我要回anotherRealmObject其中它已经因为顺便提及修改?

Is it because I'm returning anotherRealmObject wherein it is already modified because of the passing reference?

推荐答案

所有托管 RealmObjects RealmResults 属于特定领域实例。相应领域实例被关闭后, RealmObject 无效。

All managed RealmObjects and RealmResults belong to a specific Realm instance. After the corresponding Realm instance gets closed, the RealmObject becomes invalid.

象下面这样的情况下:

Realm realm = Realm.getInstance(context);
realm.beginTransaction();
MyObject obj = realm.createObject(MyObject.class);
realm.commitTransaction();
realm.close();

realm = Realm.getInstance(context);
realm.beginTransaction();
MyObject obj2 = realm.where(MyObject2.class).findFirst();
obj2.setObj1(obj); // Throws exception, because of the obj's Realm instance is closed. It is invalid now.
realm.commitTransaction();

您可以通过此的 DOC