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())
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);
Is it because I'm returning anotherRealmObject wherein it is already modified because of the passing reference?
Upon researching there is a method on realm objects to check if it is valid:
realmObject.isValid();
There are two ways I know how to instantiate a realmObject:
RealmObject realmObj = new RealObject(); //Invalid
RealmObject realmObj = realmInstance().createObject(RealmClass.class); //Valid
I was using parceler to pass realmObjects around. Passing a realmObject through parceler and unwrapping it and assigning it to a realmObject variable would make it invalid:
RealmObject realmObj = Parcels.unwrap(data.getParcelableExtra("realmObject"));
Solution 1 - Pass the unique identifer, then query the realm object:
int uniqueId = Parcels.unwrap(data.getParcelableExtra("uniqueId"));
Solution 2 - Pass the values, retrieve it, create a realmObject through a realmInstance and assign the values.
//Retrieve values
String value1 = Parcels.unwrap(data.getParcelableExtra("value1"));
String value2 = Parcels.unwrap(data.getParcelableExtra("value2"));
//Create realmObject 'properly'
RealmObject realmObj = realmInstance().createObject(RealmClass.class);
//Assign retrieved values
realmObj.setValue1(value1);
realmObj.setValue2(value2);
This way you won't get an invalid realm object.