境界异常'值'是不是有效的管理对象 [英] Realm Exception 'value' is not a valid managed object
问题描述
我设置与另一个境界对象,它是不同类的领域对象上的属性,但是我得到的错误:值是不是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
这篇关于境界异常'值'是不是有效的管理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!