java - Only valid managed objects can be copied from Realm -
i have one-to-many relationship in realm
public class bayentity extends realmobject implements realmmodel { @primarykey private int id; private string title; } public class termentity extends realmobject implements realmmodel { @primarykey private string termid; private string name; private realmlist<bayentity> baylist; public void updatebaylist(arraylist<bayentity> baylist) { if(collectionutility.isemptyornull(this.baylist)) this.baylist = new realmlist(); this.baylist.addall(baylist); realm.begintransaction(); realm.copytorealm(this.baylist); realm.insertorupdate(this); //update current instance realm.committransaction(); } public arraylist<bayentity> getsimplelist() { if(collectionutility.isemptyornull(baylist)) return new arraylist<>(); return (arraylist<bayentity>) realm.copyfromrealm(baylist); } }
i getting termentity
objects containing baylist as:
public realmresults<termentity> getterms() { return realm.where(termentity.class).findall(); }
i able save , retrieve baylist
successfully. when try convert arraylist
in method getsimplelist()
, getting exception:
java.lang.illegalargumentexception: valid managed objects can copied realm.
i can't understand how objects can unmanaged when saved , retrieved realm. please correct me. thanks.
it looks me though have several problems:
where getting
realm
instance use inupdatebaylist
method? since instance local state, might refer realm not valid in current context (time or thread)the first 3 lines
if ... addall(baylist);
modifyterm
srealmlist
. can't outside of transaction, if object managed. when object not managed, error describe, because trying add managed objects unmanagedrealmlist
while not problem,
realmobject
implementsrealmmodel
. once, why again?
edited add:
in order add bayentity
s termentity
, first make sure termentity
managed object (use copytorealm
, or query it). once that, code in updatebaylist
correct. want like:
realm.begintransaction(); if (collectionutility.isemptyornull(this.baylist)) { this.baylist = new realmlist(); } this.baylist.addall(baylist); realm.committransaction();
Comments
Post a Comment